#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];

bool dp[304][304][8]; //poslednje stanje je maska :)
int uzo[304][304]; //KOJU SAM MASKU UZO
///rEKONSTRUKCIJA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

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];

    ff(repeatthis,0,10) 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]);

        l[i-1][j-1]=max(l[i-1][j-1],l[i-1][j]+l[i][j-1]-r[i][j]);
        r[i-1][j-1]=min(r[i-1][j-1],r[i-1][j]+r[i][j-1]-l[i][j]);

        l[i-1][j]=max(l[i-1][j],l[i-1][j-1]+l[i][j]-r[i][j-1]);
        r[i-1][j]=min(r[i-1][j],r[i-1][j-1]+r[i][j]-l[i][j-1]);

        l[i][j-1]=max(l[i][j-1],l[i-1][j-1]+l[i][j]-r[i-1][j]);
        r[i][j-1]=min(r[i][j-1],r[i-1][j-1]+r[i][j]-l[i-1][j]);

        if (l[i][j]>r[i][j]) return cout<<"NE\n",0;
    }

    ff(mask,0,8) fff(i,1,n) dp[i][1][mask]=1;
    ff(mask,0,8) fff(j,1,m) dp[1][j][mask]=1;

    /*cout<<endl;
    fff(i,1,n)
    {
        fff(j,1,m) cout<<l[i][j]<<" ";
        cout<<endl;
    } cout<<endl;

    fff(i,1,n)
    {
        fff(j,1,m) cout<<r[i][j]<<" ";
        cout<<endl;
    } cout<<endl;*/

    fff(i,2,n) fff(j,2,m) ff(mask,0,8)
    {
        bool aL=(mask&1),bL=(mask&2),cL=(mask&4);
        li A=(aL?l[i][1]:r[i][1]),B=(bL?l[1][j]:r[1][j]),C=(cL?l[1][1]:r[1][1]);
        if (!(l[i][j]<=A+B-C && A+B-C<=r[i][j])) continue; //impossibel case;

        if (dp[i-1][j][mask] && dp[i][j-1][mask]) dp[i][j][mask]=true,uzo[i][j]=0;
        if (dp[i-1][j][mask] && dp[i][j-1][mask^2]) dp[i][j][mask]=true,uzo[i][j]=2;
        if (dp[i-1][j][mask^1] && dp[i][j-1][mask]) dp[i][j][mask]=true,uzo[i][j]=1;
        if (dp[i-1][j][mask^1] && dp[i][j-1][mask^2]) dp[i][j][mask]=true,uzo[i][j]=3;
    }

    bool OKEJMOZE=false;
    ff(c,0,8) if (dp[n][m][c]) OKEJMOZE=true;
    if (!OKEJMOZE) return cout<<"NE\n",0;

    ff(c,0,8) if (dp[n][m][c])
    {
        bool aL=(c&1),bL=(c&2),cL=(c&4);
        ans[1][1]=(cL?l[1][1]:r[1][1]);
        ans[1][m]=(bL?l[1][m]:r[n][1]);
        ans[n][1]=(aL?l[n][1]:r[n][1]);

        bfff(i,2,n-1)
        {
            if (uzo[i+1][m]&1) aL=!aL;
            ans[i][1]=(aL?l[i][1]:r[i][1]);
        }
        bfff(j,2,m-1)
        {
            if (uzo[n][j+1]&2) bL=!bL;
            ans[1][j]=(bL?l[1][j]:r[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];
        //fff(i,1,n) fff(j,1,m) if (ans[i][j]<l[i][j] || ans[i][j]>r[i][j]) return cout<<"NE\n",0;

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

        #define RETURNUJ return
        RETURNUJ 0;
    }

    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

*/