#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define mp make_pair
#define mt make_tuple
#define sz size()
#define pb push_back
#define SPED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (long long (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (long long (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (long long (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (long long (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)
#define popcount(x) __builtin_popcountll(x)
#define PI 3.14159265358979323846

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pll;
typedef vector<vector<ll>> graf;
typedef array<ll,3> all;
typedef array<double,3> add;

const ll mod = 1e9+7;
const ll MOD = 998244353;
const ll MAXN = 100000;
const ll INF = 1e18;
const ld EPS = 1e-9;

template <class T> using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

ll sweep(vector<pll>& a, ll n)
{
    vector<pair<ll,char>> b;
    ff(i,0,n)
    {
        b.pb({a[i].first,'l'});
        b.pb({a[i].second,'r'});
    }
    sort(all(b));
    ll br=0;
    ff(i,0,b.sz)
    {
        if(b[i].second=='l')
            br++;
        else br--;
        if(br==n)
            return b[i].first;
    }
    return INF;
}

int main()
{
    SPED;
    ll t;
    cin >> t;
    while(t--)
    {
        ll n,m;
        cin >> n >> m;
        vector<vector<ll>> mat1(n,vector<ll>(m));
        vector<vector<ll>> mat2(n,vector<ll>(m));
        vector<ll> maxred(n);
        ff(i,0,n)
        {
            ll mx = -INF;
            ff(j,0,m)
            {
                cin >> mat1[i][j];
                mx=max(mx,mat1[i][j]);
            }
            maxred[i]=mx;
        }
        vector<ll> ss(m);
        ff(j,0,m)
        ss[j]=mat1[n-1][j];
        ff(i,0,n)
        {
            ff(j,0,m)
            cin >> mat2[i][j];
        }
        vector<vector<ll>> addition(n,vector<ll>(m));
        ff(i,0,n)
        {
            bff(j,0,m)
            {
                addition[i][j]+=maxred[n-1]- ss[j];
                mat1[i][j]+=maxred[n-1]- ss[j];
                mat2[i][j]+=maxred[n-1]- ss[j];
            }
        }
        ll minn = INF;
        vector<ll> setise(n);
        ff(i,0,n){
        minn=min(minn,mat1[i][0]);
        setise[i]=mat1[i][0];
        }
        ff(i,0,n)
        {
            bff(j,0,m)
            {
                addition[i][j]-=setise[i]-minn;
                mat1[i][j]-=setise[i]-minn;
                mat2[i][j]-=setise[i]-minn;
            }
        }
        vector<vector<ll>> res(n,vector<ll>(m));
        bool moze = true;
        ff(j,0,m)
        {
            vector<pll> p;
            ff(i,0,n)
            {
                p.pb(mp(mat1[i][j],mat2[i][j]));
            }
            ll res1 = sweep(p,n);
            if(res1==INF)
            {
                moze=false;
                break;
            }
            ff(i,0,n)
            {
                res[i][j]=res1-addition[i][j];
            }
        }
        if(!moze)
        {
            cout << "NE\n";
            continue;
        }
        cout << "DA\n";
        ff(i,0,n)
        {
            ff(j,0,m)
            {
                cout << res[i][j] << " ";
            }
            cout << "\n";
        }
    }
    return 0;
}