#include <bits/stdc++.h>
using namespace std;

#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ull unsigned long long
#define inf INT_MAX
#define llinf LLONG_MAX
#define mt make_tuple
#define mp make_pair
#define parll pair<ll,ll>
#define parii pair<int,int>
#define frs first
#define sec second
#define neparno(x) (x&1)
#define parno(x) (!neparno(x))
#define lowbit(x) (x&-x)
#define nzd __gcd
#define nzs(e1,e2) (e1/nzd(e1,e2))*e2
#define all(V) V.begin(),V.end()
#define fill(V,v) fill(all(V),v)
#define l_b(BRL,brl) lower_bound( BRL.begin(), BRL.end(), brl )-BRL.begin()-1
#define u_b(BRU,bru) upper_bound( BRU.begin(), BRU.end(), bru )-BRU.begin()
#define mxn 302

ll q,n,m,A[mxn][mxn],L[mxn][mxn],U[mxn][mxn];

void Drugi()
{
    for ( ll i = 1; i <= n; i++ )
    {
        ll maxx = -llinf;
        for ( ll j = 1; j <= m; j++ )
        {
            maxx = max( maxx, L[i][j] );
        }
        for ( ll j = 1; j <= m; j++ )
        {
            A[i][j] += maxx;
        }
        for ( ll j = 1; j <= m; j++ )
        {
            if ( A[i][j] > L[i][j] )
            {
                if ( i > 1 )
                {
                    cout << "NE\n";
                    return;
                }
                ll r = A[i][j]-L[i][j];
                for ( ll k = 1; k <= n; k++ )
                {
                    A[k][j] -= r;
                }
            }
        }
    }
    cout << "DA\n";
    for ( ll i = 1; i <= n; i++ )
    {
        for ( ll j = 1; j <= m; j++ )
        {
            cout << A[i][j] << " ";
        }
        cout << "\n";
    }
}

void Treci()
{
    ll maxx = -llinf;
    for ( ll j = 1; j <= m; j++ )
    {
        maxx = max( maxx, L[1][j] );
    }
    for ( ll j = 1; j <= m; j++ )
    {
        A[1][j] += maxx;
    }
    for ( ll j = 1; j <= m; j++ )
    {
        if ( A[1][j] > L[1][j] )
        {
            ll r = A[1][j]-L[1][j];
            for ( ll k = 1; k <= n; k++ )
            {
                A[k][j] -= r;
            }
        }
    }
    for ( ll i = 2; i <= n; i++ )
    {
        ll r = L[i][1]-A[i][1];
        for ( ll j = 1; j <= m; j++ )
        {
            A[i][j] += r;
        }
    }
    for ( ll i = 1; i <= n; i++ )
    {
        for ( ll j = 1; j <= m; j++ )
        {
            if ( A[i][j] < L[i][j] || A[i][j] > U[i][j] )
            {
                cout << "NE\n";
                return;
            }
        }
    }
    cout << "DA\n";
    for ( ll i = 1; i <= n; i++ )
    {
        for ( ll j = 1; j <= m; j++ )
        {
            cout << A[i][j] << " ";
        }
        cout << "\n";
    }
}

void Reset()
{
    for ( ll i = 1; i <= n; i++ )
    {
        for ( ll j = 1; j <= m; j++ )
        {
            A[i][j] = 0;
        }
    }
}

int main()
{
    ios;

    cin >> q;
    while ( q-- )
    {
        cin >> n >> m;
        for ( ll i = 1; i <= n; i++ )
        {
            for ( ll j = 1; j <= m; j++ )
            {
                cin >> L[i][j];
            }
        }
        bool drugi=true,treci=true;
        for ( ll i = 1; i <= n; i++ )
        {
            for ( ll j = 1; j <= m; j++ )
            {
                cin >> U[i][j];
                if ( U[i][j] != L[i][j] )
                {
                    drugi = false;
                }
                if ( (i==1 || j ==1) && (U[i][j] != L[i][j]) )
                {
                    treci = false;
                }
            }
        }

        if ( drugi )
        {
            Drugi();
            Reset();
            continue;
        }
        if ( treci )
        {
            Treci();
            Reset();
            continue;
        }
        Reset();
    }

    return 0;
}