/**
 *    author:  wxhtzdy
 *    created: 17.12.2022 19:23:42
**/
#include <bits/stdc++.h>

using namespace std;

const int N = 305;

int l[N][N], r[N][N];
long long lb[N][N], ub[N][N];
long long ans[N][N];

/*
string to_string(string s) {
  return '"' + s + '"';
}
string to_string(const char* s) {
  return to_string((string) s);
}
string to_string(bool b) {
  return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
*/ 

bool was[N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int tt;
  cin >> tt;
  while (tt--) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> l[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> r[i][j];
      }
    }
    bool valid = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        lb[i][j] = l[i][j];
        ub[i][j] = r[i][j];
        was[i][j] = false;
      }
    }
    for (int it = 0; it < n + m; it++) {
      bool found = false;
      for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < m - 1; j++) {
          long long L1 = lb[i][j] + lb[i + 1][j + 1];
          long long R1 = ub[i][j] + ub[i + 1][j + 1];
          long long L2 = lb[i + 1][j] + lb[i][j + 1];
          long long R2 = ub[i + 1][j] + ub[i][j + 1];
          long long L = max(L1, L2);
          long long R = min(R1, R2);
          if (L > R) {
            valid = false;
            break;
          }
          if (L1 != L2 || R1 != R2) {
            found = true;
          }
          
          long long a_lb = max(lb[i][j], L - ub[i + 1][j + 1]);
          long long d_lb = max(lb[i + 1][j + 1], L - ub[i][j]);
          long long a_ub = min(ub[i][j], R - lb[i + 1][j + 1]);
          long long d_ub = min(ub[i + 1][j + 1], R - lb[i][j]);

          long long b_lb = max(lb[i + 1][j], L - ub[i][j + 1]);
          long long c_lb = max(lb[i][j + 1], L - ub[i + 1][j]);
          long long b_ub = min(ub[i + 1][j], R - lb[i][j + 1]);
          long long c_ub = min(ub[i][j + 1], R - lb[i + 1][j]);

          lb[i][j] = a_lb;
          ub[i][j] = a_ub;
          lb[i + 1][j + 1] = d_lb;
          ub[i + 1][j + 1] = d_ub;

          lb[i + 1][j] = b_lb;
          ub[i + 1][j] = b_ub;
          lb[i][j + 1] = c_lb;
          ub[i][j + 1] = c_ub;
        }
        if (!valid) {
          break;
        }
      }
      if (!found) {
        break;
      }
      if (!valid) {
        break;
      }
    }
    if (!valid) {
      cout << "NE" << '\n';
      continue;
    }
    if (n == 1 || m == 1) {
      cout << "DA" << '\n';
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          cout << l[i][j] << " \n"[j == m - 1];  
        }
      }
      continue;
    }         
    /* for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cout << lb[i][j] << " ";
      }
      cout << '\n';
    }
    cout << '\n';
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cout << ub[i][j] << " ";
      }
      cout << '\n';
    }
    cout << '\n'; */
    ans[0][0] = lb[0][0];                        
    long long s = max(lb[0][0] + lb[1][1], lb[1][0] + lb[0][1]);
    ans[1][1] = s - ans[0][0];
    ans[1][0] = lb[1][0];
    ans[0][1] = s - ans[1][0];
    was[0][0] = was[1][0] = was[0][1] = was[1][1] = true;
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < m - 1; j++) {
        if (i == 0 && j == 0) {
          continue;
        }                                                      
        if (was[i + 1][j]) { 
          s = max(ans[i][j] + lb[i + 1][j + 1], ans[i + 1][j] + lb[i][j + 1]);
          ans[i + 1][j + 1] = s - ans[i][j];
          ans[i][j + 1] = s - ans[i + 1][j];
          was[i + 1][j + 1] = was[i][j + 1] = true;
        } else if (was[i][j + 1]) {
          s = max(ans[i][j] + lb[i + 1][j + 1], ans[i][j + 1] + lb[i + 1][j]);
          ans[i + 1][j + 1] = s - ans[i][j];
          ans[i + 1][j] = s - ans[i][j + 1];
          was[i + 1][j + 1] = was[i + 1][j] = true;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (ans[i][j] < l[i][j] || ans[i][j] > r[i][j]) {
          valid = false;
        }
      }
    }
    for (int i = 0; i + 1 < n; i++) {
      for (int j = 0; j + 1 < m; j++) {
        if (ans[i][j] + ans[i + 1][j + 1] != ans[i + 1][j] + ans[i][j + 1]) {
          valid = false;
        }
      }
    }
    if (!valid) {
      cout << "NE" << '\n';
      continue;  
    }
    cout << "DA" << '\n';
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cout << ans[i][j] << " \n"[j == m - 1]; 
      }
    }
  }
  // debug(clock());                                                                    
  return 0;
}