#include <bits/stdc++.h>
#define ll long long
#define MAXN 310
using namespace std;
ll u[MAXN][MAXN],l[MAXN][MAXN],res[MAXN][MAXN];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll t,n,m;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        for (ll i=1;i<=n;i++)
        {
            for (ll j=1;j<=m;j++)
            {
                cin >> l[i][j];
                res[i][j]=0;
            }
        }
        bool jednaki=true;
        for (ll i=1;i<=n;i++)
        {
            for (ll j=1;j<=m;j++)
            {
                cin >> u[i][j];
                if (l[i][j]!=u[i][j])
                    jednaki=false;
            }
        }
        if (jednaki)
        {
            bool moze=true;
            for (ll i=1;i<=n;i++)
            {
                ll maxx=LLONG_MIN;
                for (ll j=1;j<=m;j++)
                    maxx=max(maxx,l[i][j]);
                for (ll j=1;j<=m;j++)
                    res[i][j]=l[i][j]-maxx;
            }
            for (ll j=1;j<=m;j++)
            {
                for (ll i=2;i<=n;i++)
                {
                    if (res[i][j]!=res[i-1][j])
                        moze=false;
                }
            }
            if (!moze)
                cout << "NE\n";
            else
            {
                cout << "DA\n";
                for (ll i=1;i<=n;i++)
                {
                    for (ll j=1;j<=m;j++)
                        cout << l[i][j] << " ";
                    cout << "\n";
                }
            }
            continue;
        }
        jednaki=true;
        for (ll i=1;i<=m;i++)
        {
            if (u[1][i]!=l[1][i])
                jednaki=false;
        }
        if (jednaki)
        {
            ll maxx=0;
            bool moze=true;
            for (ll i=1;i<=m;i++)
                maxx=max(maxx,l[1][i]);
            for (ll j=1;j<=m;j++)
            {
                for (ll i=1;i<=n;i++)
                    res[i][j]-=(maxx-l[1][j]);
            }
            for (ll i=1;i<=n;i++)
            {
                ll minn=LLONG_MIN;
                maxx=LLONG_MAX;
                for (ll j=1;j<=m;j++)
                {
                    minn=max(minn,l[i][j]-res[i][j]);
                    maxx=min(maxx,u[i][j]-res[i][j]);
                }
                if (minn>maxx || maxx<0)
                {
                    moze=false;
                    break;
                }
                for (ll j=1;j<=m;j++)
                    res[i][j]+=maxx;
            }
            if (!moze)
                cout << "NE\n";
            else
            {
                for (ll i=1;i<=n;i++)
                {
                    for (ll j=1;j<=m;j++)
                        cout << res[i][j] << " ";
                    cout << "\n";
                }
            }
            continue;
        }
    }
    return 0;
}