#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>

#define ll long long
#define matrix vector<vector<ll>>
#define red vector<ll>

using namespace ::std;

void solve()
{
    int n, m;
    cin >> n >> m;
    matrix u(n, red(m)), l(n, red(m));
    for (auto &i : l)
        for (auto &j : i)
            cin >> j;
    for (auto &i : u)
        for (auto &j : i)
            cin >> j;
    matrix mat(n, red(m, 0));
    bool podzad = true;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (i == 0 || j == 0)
                podzad = podzad || u[i][j] == l[i][j];
        }
    }
    if (podzad)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (i == 0 || j == 0)
                {
                    mat[i][j] = u[i][j];
                }
            }
        }
        if (n == 1 || m == 1)
        {
            cout << "DA" << '\n';
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    cout << mat[i][j] << ' ';
                }
                cout << '\n';
            }
            return;
        }
        vector<ll> difsX(m - 1), difsY(n - 1);
        for (int i = 1; i < m; i++)
            difsX[i - 1] = mat[0][i] - mat[0][i - 1];
        for (int i = 1; i < n; i++)
            difsY[i - 1] = mat[i][0] - mat[i - 1][0];
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < m; j++)
            {
                if (mat[i - 1][j] + difsY[i - 1] != mat[i][j - 1] + difsX[j - 1])
                {
                    cout << "NE" << '\n';
                    return;
                }
                mat[i][j] = mat[i - 1][j] + difsY[i - 1];
                if (mat[i][j] < l[i][j] || mat[i][j] > u[i][j])
                {
                    cout << "NE" << '\n';
                    return;
                }
            }
        }
        cout << "DA" << '\n';
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cout << mat[i][j] << ' ';
            }
            cout << '\n';
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}