#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}

#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);

using namespace std;
//using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
    x+=y;
    if(x<0){
        x%=mod;
        x+=mod;
    }else{
        if(x>=mod) x%=mod;
    }
    return x;
}
ll mul(ll a,ll b){
	ll ans = (a*b)%mod;
	if(ans<0) ans+=mod;
	return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
    return uniform_int_distribution<ll>(l,r)(rng);
}
*/
#define maxn 305
ll n,m;
ll l[maxn][maxn],r[maxn][maxn],a[maxn][maxn],e[maxn][maxn];
ll b[maxn],c[maxn],b2[maxn],c2[maxn];
ll add[3][3],mx[3][3],mi[3][3];
ll mid[3],mxd[3];
vector<ll> v;
bool naso = 0;
void reshi(ll cur){
    if(naso) return;
    if(cur==n+m-1){
        ll it = 0;
        for(ll i = 1;i<=m;i++){
            if(v[it]) e[1][i] = l[1][i];
            else e[1][i] = r[1][i];
            it++;
        }
        for(ll i = 2;i<=n;i++){
            if(v[it]) e[i][1] = l[i][1];
            else e[i][1] = r[i][1];
            it++;
        }
        for(ll i = 1;i<=n;i++) for(ll j = 1;j<=m;j++){
            if(i==1||j==1) a[i][j] = l[i][j];
            else{
                a[i][j] = l[i][1] + l[1][j] - l[1][1];
            }
        }
        bool ok = 1;
        for(ll i = 1;i<=n;i++){
            for(ll j = 1;j<=m;j++){
                if(a[i][j]<l[i][j]||a[i][j]>r[i][j]) ok = 0;
            }
        }
        if(ok){
            cout<<"DA"<<endl;
            for(ll i = 1;i<=n;i++){
                for(ll j = 1;j<=m;j++) cout<<a[i][j]<< " ";
                cout<<endl;
            }
            naso = 1;
        }
        return;
    }
    v.pb(1);
    reshi(cur+1);
    v.popb();
    if(naso) return;
    v.pb(0);
    reshi(cur+1);
    v.popb();
}
void tc(){
    cin >> n >> m;
    bool ok1 = 1,ok2 = 1,ok3 = 1;
    for(ll i = 1;i<=n;i++) for(ll j = 1;j<=m;j++) cin >> l[i][j];
    for(ll i = 1;i<=n;i++) for(ll j = 1;j<=m;j++) cin >> r[i][j];
    for(ll i = 1;i<=n;i++) ok1&=(l[i][1]==r[i][1]);
    for(ll i = 1;i<=m;i++) ok2&=(l[1][i]==r[1][i]);
    ok3&=(n<=10);
    ok3&=(m<=10);
    for(ll i = 1;i<=n;i++){
        for(ll j = 1;j<=m;j++){
            if((i&1)&&(j&1)){
                ok2&=(l[i][j]==r[i][j]);
            }
            ok3&=(abs(l[i][j]-r[i][j])<=1);
        }
    }
    if(n==1||m==1){
        cout<<"DA"<<endl;
        return;
    }
    if(ok1){
        for(ll i = 1;i<=n;i++) for(ll j = 1;j<=m;j++){
            if(i==1||j==1) a[i][j] = l[i][j];
            else{
                a[i][j] = l[i][1] + l[1][j] - l[1][1];
            }
        }
        bool ok = 1;
        for(ll i = 1;i<=n;i++){
            for(ll j = 1;j<=m;j++){
                if(a[i][j]<l[i][j]||a[i][j]>r[i][j]) ok = 0;
            }
        }
        if(ok){
            cout<<"DA"<<endl;
            for(ll i = 1;i<=n;i++){
                for(ll j = 1;j<=m;j++) cout<<a[i][j]<< " ";
                cout<<endl;
            }
        }else cout<<"NE"<<endl;
        return;
    }else if(ok2){
        ll mini = llinf;
        for(ll i = 1;i<=m;i+=2) mini = min(mini,l[1][i]);
        for(ll i = 1;i<=m;i+=2) c[i] = l[1][i]-mini;
        mini = llinf;
        for(ll i = 2;i<=m;i+=2) mini = min(mini,l[1][i]);
        for(ll i = 2;i<=m;i+=2) c2[i] = l[1][i]-mini;
        mini = llinf;
        for(ll i = 1;i<=n;i+=2) mini = min(mini,l[i][1]);
        for(ll i = 1;i<=n;i+=2) b[i] = l[i][1]-mini;
        mini = llinf;
        for(ll i = 2;i<=n;i+=2) mini = min(mini,l[i][1]);
        for(ll i = 2;i<=n;i+=2) b2[i] = l[i][1]-mini;
        for(ll i = 1;i<=n;i++) for(ll j = 1;j<=m;j++){
            if(i&1) a[i][j]+=b[i];
            else a[i][j]+=b2[i];
            if(j&1) a[i][j]+=c[j];
            else a[i][j]+=c2[j];
        }
        add[1][1] = l[1][1]-a[1][1];
        add[0][0] = l[2][2]-a[2][2];
        ll d = add[1][1]+add[0][0];
        vector<ll> v[2];
        mid[0] = mid[1] = 0;
        mxd[0] = mxd[1] = llinf;
        for(ll i = 1;i<=n;i++){
            for(ll j = 1;j<=m;j++){
                if((i&1)==(j&1)) continue;
                mx[i&1][j&1] = r[i][j]-a[i][j];
                mi[i&1][j&1] = l[i][j]-a[i][j];
                mid[i&1] = max(mid[i&1],mi[i&1][j&1]);
                mxd[i&1] = min(mxd[i&1],mx[i&1][j&1]);
            }
        }
        if(mid[0]>mxd[0]){
            cout<<"NE"<<endl;
            return;
        }
        if(mid[1]>mxd[1]){
            cout<<"NE"<<endl;
            return;
        }
        mid[0] = d-mid[0];
        mxd[0] = d-mxd[0];
        ll l0 = mid[0],r0 = mxd[0];
        ll l1 = mid[1],r1 = mxd[1];
        if(l0>l1){
            swap(l0,l1);
            swap(r0,r1);
        }
        if(l1<=r0){
            cout<<"DA"<<endl;
            add[1][0] = l1;
            add[0][1] = d-l1;
            for(ll i = 1;i<=n;i++){
                for(ll j = 1;j<=m;j++){
                    a[i][j]+=add[i&1][j&1];
                    cout<<a[i][j]<< " ";
                }
                cout<<endl;
            }
            return;
        }else cout<<"NE"<<endl;
        return;
    }else if(ok3){
        naso = 0;
        reshi(0);
        if(!naso) cout<<"NE"<<endl;
        return;
    }else{
        //if(rnd(0,1)==1) cout<<"DA"<<endl;
        //else cout<<"NE"<<endl;
    }
}
int main(){
	daj_mi_malo_vremena
    int t; t = 1;
    cin >> t;
    while(t--){
        tc();
    }
	return 0;
}