#include<bits/stdc++.h>
using namespace std;
int n,w,h;
int** t1;
int** t2;
int** res;
int* mins;
int* maxs;
bool ok;
int main(){
    cin>>n;
    while(n--){
        cin>>h>>w;
        t1=new int*[w];
        t2=new int*[w];
        res=new int*[w];
        for(int i=0;i<w;i++){
            t1[i]=new int[h];
            t2[i]=new int[h];
            res[i]=new int[h];
        }
        mins=new int[w];
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++){
                cin>>t1[j][i];
                if(j==0) mins[j]=t1[j][0];
                else if(t1[j][i]<mins[j]) mins[j]=t1[j][i];
            }
        maxs=new int[h];
        for(int i=0;i<h;i++)
            for(int j=0;j<w;j++){
                cin>>t2[j][i];
                if(j==0) maxs[i]=t2[0][i];
                else if(t2[j][i]>maxs[i]) maxs[i]=t2[j][i];
            }

        ok=true;

        for(int i;i<w;i++){
            for(int j;j<h;i++){
                res[i][j]=maxs[i]+mins[j];
                if(res[i][j]<t1[i][j]||res[i][j]>t2[i][j]){
                    ok=false;
                    break;
                }
            }
            if(!ok) break;
        }

        if(ok){
            cout<<"DA"<<endl;
            for(int i=0;i<h;i++){
                for(int j=0;j<w;j++)
                    cout<<res[j][i]<<" ";
                cout<<endl;
            }
        }else
            cout<<"NE"<<endl;

        for(int i=0;i<w;i++){
            delete t1[i];
            delete t2[i];
            delete res[i];
        }
        delete t1;
        delete t2;
        delete res;
    }
    return 0;
}