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

void povecaj_kol(vector<vector<int>>& vek,int broj,int m){
    for(int i=0;i<m;i++){
        vek[i][m]+=broj;
    }
    //return vek;
}

void povecaj_red(vector<vector<int>>& vek,int broj,int n){
    for(int i=0;i<n;i++){
        vek[n][i]+=broj;
    }
    //return vek;
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t;
    cin >> t;

    while(t){

    bool vre=true;
    int n,m;
    cin >> n >> m;

    vector<vector<int>> lazar(n);
    vector<vector<int>> momir(n);
    vector<vector<int>> uros(n);
    vector<vector<pair<int,int>>> sred(n);

    for(int i=0;i<n;i++){
        lazar[i].resize(m);
        for(int j=0;j<m;j++){
            cin >> lazar[i][j];
        }
    }
    for(int i=0;i<n;i++){
        uros[i].resize(m);
        for(int j=0;j<m;j++){
            cin >> uros[i][j];
        }
    }
    int minbr=INT_MAX;
    //int maxbr=INT_MIN;
    for(int i=0;i<n;i++){
        sred[i].resize(m);
        for(int j=0;j<m;j++){
            sred[i][j].first=min(lazar[i][j],uros[i][j]);
            sred[i][j].second=max(lazar[i][j],uros[i][j]);
            minbr=min(minbr,sred[i][j].first);
            //maxbr=max(minbr,sred[i][j].second);
        }
    }

    for(int i=0;i<n;i++){
        momir[i].resize(m);
        for(int j=0;j<m;j++){
            momir[i][j]=sred[i][j].first;
        }
    }
    /*
    vector<vector<int>> probni(n);

    for(int i=0;i<n;i++){
        probni[i].resize(m);
        for(int j=0;j<m;j++){
            probni[i][j]=minbr;
        }
    }*/
    //...
    //int broj=0;
    for(int j=0;j<m;j++){
        if(momir[0][j]<sred[0][j].first){povecaj_kol(momir,sred[0][j].first - momir[0][j],0);}
    }

    for(int i=1;i<n;i++){
        for(int j=0;j<m;j++){
            if(momir[i][j]<sred[i][j].first){povecaj_red(momir,sred[i][j].first - momir[i][j],i);}
        }
    }

    for(int i=1;i<n;i++){
        for(int j=0;j<m-1;j++){
            if(momir[i][j] - momir[i-1][j] != momir[i][j+1] - momir[i-1][j+1] ){vre=false;break;}
        }
    }

    //...
    if(vre){
        cout<<"DA"<<"\n";
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cout<<momir[i][j]<<" ";
            }
            cout << "\n";
        }
    }
    else{
        cout<<"NE"<<"\n";
    }
    t--;
    }
    return 0;
}