using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5.zadatak
{
    internal class Program
    {
        struct Matrica
        {
            public bool moguce;
            public int[,] momir;
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            Matrica[] matrice = new Matrica[t];
            for (int br = 0; br < t; br++)
            {
                string[] s = Console.ReadLine().Split();
                int n = int.Parse(s[0]), m = int.Parse(s[1]);
                int[,] lazar = new int[n, m];
                for (int i = 0; i < n; i++)
                {
                    s = Console.ReadLine().Split();
                    for (int j = 0; j < m; j++)
                    {
                        lazar[i, j] = int.Parse(s[j]);
                    }
                }
                int[,] ugljesa = new int[n, m];
                for (int i = 0; i < n; i++)
                {
                    s = Console.ReadLine().Split();
                    for (int j = 0; j < m; j++)
                    {
                        ugljesa[i, j] = int.Parse(s[j]);
                    }
                }
                matrice[br].momir = new int[n, m];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        while (matrice[br].momir[i, j] < lazar[i, j])
                        {
                            for (int k = 0; k < m; k++)
                            {
                                matrice[br].momir[i, k]++;
                            }
                        }
                        while (matrice[br].momir[i, j] > ugljesa[i, j])
                        {
                            for (int k = 0; k < n; k++)
                            {
                                matrice[br].momir[k, j]--;
                            }
                        }
                    }
                }
                matrice[br].moguce = true;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (matrice[br].momir[i, j] < lazar[i, j] || matrice[br].momir[i, j] > ugljesa[i, j])
                        {
                            matrice[br].moguce = false;
                            break;
                        }
                    }
                    if (!matrice[br].moguce) break;
                }
            }
            for (int br = 0; br < t; br++)
            {
                if (matrice[br].moguce)
                {
                    Console.WriteLine("DA");
                    for (int i = 0; i < matrice[br].momir.GetLength(0); i++)
                    {
                        for (int j = 0; j < matrice[br].momir.GetLength(1); j++)
                        {
                            Console.Write(matrice[br].momir[i, j] + " ");
                        }
                        Console.WriteLine();
                    }
                }
                else Console.WriteLine("NE");
            }
        }
    }
}