#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b)

int laza[10][300][300];
int uglj[10][300][300];
int momi[300][300];
int n[10], m[10];

int main()
{
    int t, i, j, k;
    bool ok;
    scanf("%d", &t);

    for (k = 0; k < t; k++)
    {
        scanf("%d%d", n + k, m + k);

        for (i = 0; i < n[k]; i++)
        {
            for (j = 0; j < m[k]; j++)
                scanf("%d", laza[k][i] + j);
        }

        for (i = 0; i < n[k]; i++)
        {
            for (j = 0; j < m[k]; j++)
                scanf("%d", uglj[k][i] + j);
        }
    }

    for (k = 0; k < t; k++)
    {
        ok = true;

        for (i = 0; i < n[k] - 1; i++)
        {
            for (j = 0; j < m[k]; j++)
            {
                if (laza[k][i][j] - laza[k][i + 1][j] != laza[k][i][0] - laza[k][i + 1][0]) {
                    ok = false;
                    goto vidi_za_ugljesu;
                }
            }
        }

        for (j = 0; j < m[k] - 1; j++)
        {
            for (i = 0; i < n[k]; i++)
            {
                if (laza[k][i][j] - laza[k][i][j + 1] != laza[k][0][j] - laza[k][0][j + 1]) {
                    ok = false;
                    goto vidi_za_ugljesu;
                }
            }
        }

        if (ok)
        {
            printf("DA\n");

            for (int i = 0; i < n[k]; i++) {
                for (int j = 0; j < m[k]; j++)
                    printf("%d ", laza[k][i][j]);

                putchar('\n');
            }

            continue;
        }

        vidi_za_ugljesu:

        ok = true;

        for (i = 0; i < n[k] - 1; i++)
        {
            for (j = 0; j < m[k]; j++)
            {
                if (uglj[k][i][j] - uglj[k][i + 1][j] != uglj[k][i][0] - uglj[k][i + 1][0]) {
                    ok = false;
                    goto nista;
                }
            }
        }

        for (j = 0; j < m[k] - 1; j++)
        {
            for (i = 0; i < n; i++)
            {
                if (uglj[k][i][j] - uglj[k][i][j + 1] != uglj[k][0][j] - uglj[k][0][j + 1]) {
                    ok = false;
                    goto nista;
                }
            }
        }

        if (ok)
        {
            printf("DA\n");

            for (int i = 0; i < n[k]; i++) {
                for (int j = 0; j < m[k]; j++)
                    printf("%d ", uglj[k][i][j]);

                putchar('\n');
            }

            continue;
        }

        nista:

        int max = INT32_MIN; //max razlika
        int min = INT32_MAX; //min razlika
        int razlika;

        for (i = 0; i < n[k]; i++)
        {
            for (j = 0; j < m[k]; j++)
            {
                razlika = uglj[k][i][j] - laza[k][i][j];
                min = MIN(min, razlika);
                max = MAX(max, razlika);

                if (min == 0 && max != 0) {
                    goto nee;
                }
            }

            max = INT32_MIN;
            min = INT32_MAX;
        }

        printf("DA\n");

        for (i = 0; i < n[k]; i++)
        {
            for (j = 0; j < m[k]; j++)
            {
                momi[i][j] = laza[k][i][j];
            }
        }

        for (int b = 0; b < n[k] + 3 * m[k]; b++)
        {
            int razlika;
            for (i = 0; i < n[k] - 1; i++)
            {
                for (j = 0; j < m; j++)
                {
                    if (j == 0)
                    {
                        razlika = momi[i][j] - momi[i + 1][j];
                        continue;
                    }

                    while (momi[i][j] - momi[i + 1][j] != razlika)
                    {
                        momi[i][j] < momi[i + 1][j] ? momi[i][j]++ : momi[i + 1][j]++;
                    }
                }
            }
        }

        printf("DA\n");

        for (i = 0; i < n[k]; i++)
        {
            for (j = 0; j < m[k]; j++)
            {
                printf("%d ", momi[i][j]);
            }

            putchar('\n');
        }

        nee:
        printf("NE\n");
    }
}