#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) (x).begin(),(x).end()
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (int (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (int (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (int (i) = (b); (i) >= (a); (i)--)
#define sum_overflow(a,b) __builtin_add_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)
#define mul_overflow(a,b) __builtin_mul_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const int mod = 998244353;
//const int mod = 1000000007;







//Note to self: Check for overflow

bool moze;
li l[304][304],r[304][304];
li ans[304][304];

void oslobodi(int x, int y, int n, int m)
{
    fff(i,1,n) swap(l[i][y],l[i][m]),swap(r[i][y],r[i][m]);
    fff(j,1,m) swap(l[x][j],l[n][j]),swap(r[x][j],r[n][j]);

    fff(i,2,n) fff(j,2,m)
    {
        l[i][j]=max(l[i][j],l[i-1][j]+l[i][j-1]-r[i-1][j-1]);
        r[i][j]=min(r[i][j],r[i-1][j]+r[i][j-1]-l[i-1][j-1]);
        if (l[i][j]>r[i][j]) moze=false;
    }

    fff(i,1,n) swap(l[i][y],l[i][m]),swap(r[i][y],r[i][m]);
    fff(j,1,m) swap(l[x][j],l[n][j]),swap(r[x][j],r[n][j]);

    r[x][y]=l[x][y];
}

int solve()
{
    moze=true;

    int n,m; cin>>n>>m;
    fff(i,1,n) fff(j,1,m) cin>>l[i][j];
    fff(i,1,n) fff(j,1,m) cin>>r[i][j];

    fff(i,1,n) oslobodi(i,1,n,m),ans[i][1]=l[i][1];
    fff(j,2,m) oslobodi(1,j,n,m),ans[1][j]=l[1][j];
    fff(i,2,n) fff(j,2,m) ans[i][j]=ans[i-1][j]+ans[i][j-1]-ans[i-1][j-1];

    if (!moze) return cout<<"NE\n",0;

    cout<<"DA\n";
    fff(i,1,n)
    {
        fff(j,1,m) cout<<ans[i][j]<<" ";
        cout<<"\n";
    }

    return 0;
}

int main()
{
    FAST;

    int t; cin>>t;
    while (t--) solve();
}

/*

2
2 2
1 -2
0 -3
4 0
1 -2
2 2
0 0
0 1
0 0
0 1

*/