#include <iostream>
#include<algorithm>

using namespace std;


int main()
{
    int t;
    cin >> t;
    long long int n1[t];
    long long int m1[t];
    long long int c[310][310][t];
    bool imposible[t];
    for (int l = 0; l < t; l++){
        imposible[l] = false;
    }
    
    
    for (int l = 0; l < t; l++){
        bool stop = false;
        long long int n, m;
        cin >> n >> m;
        long long int laz[n][m];
        long long int ug[n][m];
        n1[l] = n;
        m1[l] = m;
        
        for (int i = 0; i < n; i++){
            for (int s = 0; s < m; s++){
                cin >> laz[i][s];
                c[i][s][l] = 0;
            }
        }
        for (int i = 0; i < n; i++){
            for (int s = 0; s < m; s++){
                cin >> ug[i][s];
            }
        }
        int minimumA = 100000000;
        int minimumB = 100000000;
        int failC = 30;
        
        while(!stop){
            int addweight[n];
            int removeweight[m];
            
            int maxA;
            int maxR;
            
            if (maxA < minimumA){
                minimumA = maxA;
                failC = 30;
            }
            if (maxR < minimumB){
                minimumB = maxR;
                failC = 30;
            }
            
            int currentWA = -100000;
            int currentWB = -100000;
            for (int i = 0; i < n; i++){
                addweight[i] = 0;
                for (int s = 0; s < m; s++){
                    addweight[i] += ug[i][s] - c[i][s][l];
                }
            }
            
            for (int i = 0; i < n; i++){
                removeweight[i] = 0;
                for (int s = 0; s < m; s++){
                    removeweight[i] += c[s][i][l] - laz[s][i];
                }
            }
            for (int i = 0; i < n; i++){
                if (addweight[i] > currentWA){
                    currentWA = addweight[i];
                    maxA = i;
                }
            }
            for (int i = 0; i < m; i++){
                if (removeweight[i] > currentWB){
                    currentWB = removeweight[i];
                    maxR = i;
                }
            }
            if (currentWA > currentWB){
                for (int i = 0; i < m; i++){
                    c[maxA][i][l] += 1;
                }
                   // cout << "Dodaj red:" << maxA << endl;
            }
            else{
                for (int i = 0; i < n; i++){
                    c[i][maxR][l] -= 1;
                }
               // cout << "Oduzmi kolonu:" << maxR << endl;
            }
            
            stop = true;
            failC--;
            
            for (int i = 0; i < n; i++){
                for (int s = 0; s < m; s++){
                    if (laz[i][s] <= c[i][s][l] && c[i][s][l] <= ug[i][s]){
                        
                    }
                    else{
                        stop = false;
                    }
                }
            }
            if (failC < 0){
                imposible[l] = true;
                stop = true;
            }
           // cout << "addweight:" << currentWA << endl;
           // cout << "removeweight" << currentWB << endl;
        }
    }
    for (int l = 0; l < t; l++){
        if (!imposible[l]){
            cout << "DA" << endl;
            for (int i = 0; i < n1[l]; i++){
                    for (int s = 0; s < m1[l]; s++){
                        cout << c[i][s][l] << " ";
                    }
                    cout << endl;
            }
        }
        else{
            cout << "NE" << endl;
        }
    }
    
}