#include <bits/stdc++.h>
#define FAST {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define FILES {freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}
#define ll long long
#define ull unsigned long long
#define pqueue priority_queue
#define pb push_back
#define fi first
#define se second
#define ld long double
#define pii pair<int,int>
#define pll pair<long long,long long>
#define all(a) (a).begin(), (a).end()
#define mp make_pair 
using namespace std;
// order_of_key -> # less than k
// find_by_order -> k-th element
// pq max element
 
const double eps = 0.00000001;
const int NMAX = 1e6+10;
const ll inf = LLONG_MAX/3;
const ll modi = 1e9+7;

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 300; // Maximum number of rows and columns

ll A[N][N], B[N][N], C[N][N]; // Matrices A, B, and C
int n, m; // Number of rows and columns

void solve() {
// Read input
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> A[i][j];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> B[i][j];

    // Initialize C with all zeros
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            C[i][j] = 0;

    // Iterate over elements of A and B
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            // Increase values in row i if necessary
            if (C[i][j] < A[i][j])
            {
                int diff = A[i][j] - C[i][j];
                for (int k = 0; k < m; k++)
                    C[i][k] += diff;
            }
            // Decrease values in column j if necessary
            if (C[i][j] > B[i][j])
            {
                int diff = C[i][j] - B[i][j];
                for (int k = 0; k < n; k++)
                    C[k][j] -= diff;
            }
        }
    }

    // Check if all elements in C are within the desired range
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (C[i][j] < A[i][j] || C[i][j] > B[i][j])
            {
                cout << "NE" << endl;
                return;
            }
        }
    }

    cout << "DA" << endl;

    // Check if all elements in C are within the desired range
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << C[i][j] << ' ';
        }
        cout << endl;
    }
}

int main () { 
    FAST
    
    


    int t=1;

    //*
    cin >> t;
    //*/

    int i = 1;
    while(t--) {
        //cout << "Case #" << i++ << ": " ;
        solve();
    }
}