#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))

using namespace std;

const ll LINF = 4e18;
const int mxN = 5e2+10, INF = 2e9;
ll n, m, l[mxN][mxN], r[mxN][mxN], mat[mxN][mxN], R[mxN], C[mxN], k[mxN];

void Generate(){

    for (int i = 0; i < m-1; i++)
        k[i] = R[i+1] - R[i];
    for (int i = 0; i < n; i++) mat[i][0] = C[i];
    for (int i = 0; i < n; i++)
        for (int j = 1; j < m; j++)
            mat[i][j] = mat[i][j-1] + k[j-1];
}

void Fix(int mask){

    R[0] = C[0] = (mask&1 ? r[0][0] : l[0][0]);
    for (int i = 1; i < m; i++)
        R[i] = ((mask>>i)&1 ? r[0][i] : l[0][i]);
    for (int i = 1; i < n; i++)
        C[i] = ((mask>>(m-1+i))&1 ? r[i][0] : l[i][0]);
    Generate();
}

bool Check(){

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (mat[i][j] < l[i][j] || mat[i][j] > r[i][j])
                return 0;
    return 1;
}

bool Brute(){

    for (int mask = 0; mask < (1<<(n+m-1)); mask++){
        Fix(mask);
        if (Check()) return 1;
    }
    return 0;
}

bool Fixed(){

    R[0] = C[0] = l[0][0];
    for (int i = 1; i < m; i++)
        R[i] = l[0][i];
    for (int i = 1; i < n; i++)
        C[i] = l[i][0];
    Generate();
    return Check();
}

array<ll, 2> a[mxN][mxN];

array<ll, 2> operator+(const array<ll, 2>& a, const array<ll, 2>& b){ return {a[0]+b[0], a[1]+b[1]}; }
array<ll, 2> operator-(const array<ll, 2>& a, const array<ll, 2>& b){ return {a[0]-b[0], a[1]-b[1]}; }

struct Segment{

    ll l, r;
    Segment(ll _l = 0, ll _r = 0){ l = _l; r = _r; }
    Segment operator-(ll x) const { return Segment(l-x, r-x); }
    Segment operator*(ll x) const { Segment s(l*x, r*x); if (x<0) swap(s.l, s.r); return s; }
    Segment operator&(const Segment& x){ return Segment(max(l, x.l), min(r, x.r)); }
};

bool Chess(){

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if ((i&1) == (j&1)) a[i][j] = {0, l[i][j]};
    a[1][0] = {1, 0};
    for (int i = 1; i < m; i += 2)
        a[0][i] = a[1][i] - (a[1][0] - a[0][0]);
    for (int i = 1; i < n; i++){
        if ((i^1) && (i&1))
            a[i][0] = a[i-1][0] + (a[i][1] - a[i-1][1]);
        for (int j = 1 + (i&1); j < m; j += 2)
            a[i][j] = a[i-1][j] + (a[i][j-1] - a[i-1][j-1]);
    }
    Segment seg(l[1][0], r[1][0]);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            if ((i&1)^(j&1)){
                Segment temp = (Segment(l[i][j], r[i][j]) - a[i][j][1]) * a[i][j][0];
                seg = seg&temp;
            }
        }
    }
    if (seg.l > seg.r) return 0;
    ll x = seg.l;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            mat[i][j] = a[i][j][0] * x + a[i][j][1];
    return 1;
}

void Solve(){

    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 f = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if ((i&1 == j&1) && l[i][j]^r[i][j])
                f = 0;
    bool ans;
    if (n <= 10 && m <= 10) ans = Brute();
    else if (f) ans = Chess();
    else ans = Fixed();
    if (ans){
        cout << "DA" << en;
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++)
                cout << mat[i][j] << sp;
            cout << en;
        }
    }
    else cout << "NE" << en;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    cin >> t;
    while (t--)
        Solve();

    return 0;
}