Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

You are given two matrices $A$ and $B$, each with $R$ rows (numbered $1$ through $R$) and $C$ columns (numbered $1$ through $C$). Let's denote an element of $A$ or $B$ in row $i$ and column $j$ by $A_{i,j}$ or $B_{i,j}$ respectively.

You are also given an integer $X$. You may perform the following operation on $A$ any number of times:
Choose an integer $v$.
Choose $X$ consecutive elements of $A$, either in the same row or in the same column.
Add $v$ to each of the chosen elements of $A$.

Determine whether it is possible to change $A$ to $B$ in a finite number of operations.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains three space-separated integers $R$, $C$ and $X$.
$R$ lines follow. For each valid $i$, the $i$-th of these lines contains $C$ space-separated integers $A_{i,1}, A_{i,2}, \ldots, A_{i,C}$.
$R$ more lines follow. For each valid $i$, the $i$-th of these lines contains $C$ space-separated integers $B_{i,1}, B_{i,2}, \ldots, B_{i,C}$.

------  Output ------
For each test case, print a single line containing the string "Yes" if there is a sequence of operations that changes the matrix $A$ to $B$, or "No" if such a sequence of operations does not exist.

------  Constraints ------
$1 ≤ T ≤ 10^{3}$
$2 ≤ R,C ≤ 10^{3}$
$2 ≤ X ≤ \min(R,C)$
$|A_{i,j}|, |B_{i,j}| ≤ 10^{9}$ for each valid $i,j$
the sum of $R$ over all test cases does not exceed $10^{3}$
the sum of $C$ over all test cases does not exceed $10^{3}$

------  Subtasks ------
Subtask #1 (5 points): $X=2$

Subtask #2 (10 points): $X=3$

Subtask #3 (85 points): original constraints

----- Sample Input 1 ------ 
3

2 2 2

1 2

0 1

0 0

0 0

2 2 2

1 2

0 1

0 0

-1 0

3 2 2

1 1

2 2

3 3

1 0

2 0

3 0
----- Sample Output 1 ------ 
Yes

No

No
----- explanation 1 ------ 
Example case 1: We can add $-1$ to both elements in row $1$ and add $-1$ to both elements in column $2$.    

Example case 2: After any operation, the sum of elements of $A$ remains even. However, the sum of elements of $B$ is odd, so $A$ cannot be changed to $B$.

Difficulty: HARD
Tags: Constructive algorithms

Starter code:

Solutions:
for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	(a, b) = ([], [])
	for i in range(r):
		a.append(list(map(int, input().split())))
	for i in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		for j in range(c):
			a[i][j] -= b[i][j]
	for i in range(x - 1):
		k = i + x
		while k < r:
			for j in range(c):
				a[i][j] += a[k][j]
			k += x
	k = x - 1
	while k < r:
		for i in range(x - 1):
			for j in range(c):
				a[i][j] -= a[k][j]
		k += x
	for j in range(x - 1):
		k = j + x
		while k < c:
			for i in range(x - 1):
				a[i][j] += a[i][k]
			k += x
	k = x - 1
	while k < c:
		for j in range(x - 1):
			for i in range(x - 1):
				a[i][j] -= a[i][k]
		k += x
	ans = 'Yes'
	for i in range(x - 1):
		for j in range(x - 1):
			if a[i][j] != 0:
				ans = 'No'
				break
		if ans == 'No':
			break
	print(ans)


def c(M, C, R, X):
	for i in range(R - X + 1, R):
		for j in range(C - X + 1, C):
			if X == 2:
				if sum([sum([M[p][q] * (-1) ** (R + C - p - q) for q in range(C)]) for p in range(R)]) != 0:
					print('No')
					break
			elif sum([sum([M[p][q] - M[p][q + j - C + X] - M[p + i - R + X][q] + M[p + i - R + X][q + j - C + X] for q in range(C % X, j, X)]) for p in range(R % X, i, X)]) != 0:
				print('No')
				break
		else:
			continue
		break
	else:
		print('Yes')
T = int(input())
for n in range(T):
	(R, C, X) = [int(j) for j in input().split(' ')]
	A = []
	B = list(A)
	for i in range(R):
		A.append([int(j) for j in input().split(' ')])
	for i in range(R):
		B.append([int(j) for j in input().split(' ')])
	M = [[A[i][j] - B[i][j] for j in range(C)] for i in range(R)]
	c(M, C, R, X)


t = int(input())
for _ in range(t):
	(R, C, X) = map(int, input().split())
	A = []
	for i in range(R):
		A.append(list(map(int, input().split())))
	B = []
	for i in range(R):
		B.append(list(map(int, input().split())))
	diff = []
	for i in range(R):
		diff.append([])
		for j in range(C):
			diff[i].append(B[i][j] - A[i][j])
	group_sums = []
	for i in range(X):
		group_sums.append([0] * X)
	for i in range(R):
		for j in range(C):
			group_sums[i % X][j % X] += diff[i][j]
	for i in range(X):
		integer = group_sums[i][0]
		for j in range(X):
			group_sums[i][j] -= integer
	for j in range(1, X):
		integer = group_sums[0][j]
		for i in range(X):
			group_sums[i][j] -= integer
	for i in group_sums:
		for j in i:
			if j != 0:
				print('No')
				break
		else:
			continue
		break
	else:
		print('Yes')


t = int(input())
for _ in range(t):
	(r, c, X) = map(int, input().split())
	a = []
	for i in range(r):
		a.append(list(map(int, input().split())))
	b = []
	for i in range(r):
		b.append(list(map(int, input().split())))
	crr = []
	for i in range(X):
		crr.append([0 for i in range(X)])
	for i in range(r):
		for j in range(c):
			crr[i % X][j % X] += a[i][j]
			crr[i % X][j % X] -= b[i][j]
	for i in range(X):
		tmp = crr[i][0]
		for j in range(X):
			crr[i][j] -= tmp
	for i in range(X):
		tmp = crr[0][i]
		for j in range(X):
			crr[j][i] -= tmp
	ans = True
	for i in range(X):
		for j in range(X):
			if crr[i][j] != 0:
				ans = False
				break
	if ans:
		print('Yes')
	else:
		print('No')


import numpy as np
t = int(input())
for test in range(t):
	(r, c, x) = map(int, input().split())
	a = []
	b = []
	for i in range(r):
		a.append(list(map(int, input().split())))
	for i in range(r):
		b.append(list(map(int, input().split())))
	A = np.array(a)
	B = np.array(b)
	d = np.array(A) - np.array(B)
	row = np.ones((1, x))
	for i in range(0, r):
		for j in range(0, c - x + 1):
			d[i, j:x + j] = d[i, j:x + j] - d[i, j] * row
	for j in range(c - x + 1, c):
		for i in range(0, r - x + 1):
			d[i:i + x, j] = d[i:i + x, j] - d[i, j] * row
	if np.max(np.max(d)) == 0 and np.min(np.min(d)) == 0:
		print('Yes')
	else:
		print('No')


def solution(a, b, r, c, x):
	for i in range(r):
		dev = [0]
		sku = []
		for j in range(c - x + 1):
			dev.append(a[i][j] - b[i][j])
			sku.append(a[i][j] - b[i][j] - dev[-2])
			if len(dev) == x:
				dev.pop(0)
			if len(sku) > x:
				sku[-1] += sku[-x - 1]
			if a[i][j] != b[i][j]:
				a[i][j] = b[i][j]
		sku = sku[-x:]
		ded = dev.pop()
		for k in range(c - x + 1, c):
			if k - x >= 0:
				ded = ded - sku.pop(0)
			a[i][k] -= ded
	for i in range(c - x + 1, c):
		dev = [0]
		sku = []
		for j in range(r - x + 1):
			dev.append(a[j][i] - b[j][i])
			sku.append(a[j][i] - b[j][i] - dev[-2])
			if len(dev) == x:
				dev.pop(0)
			if len(sku) > x:
				sku[-1] += sku[-x - 1]
			if a[j][i] != b[j][i]:
				a[j][i] = b[j][i]
		sku = sku[-x:]
		ded = dev.pop()
		for k in range(r - x + 1, r):
			if k - x >= 0:
				ded = ded - sku.pop(0)
			a[k][i] -= ded
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			if a[i][j] != b[i][j]:
				return 'No'
	return 'Yes'

def main():
	for cases in range(int(input())):
		(r, c, x) = map(int, input().split())
		a = list()
		b = list()
		for i in range(r):
			a.append([int(x) for x in input().split()])
		for i in range(r):
			b.append([int(x) for x in input().split()])
		ans = solution(a, b, r, c, x)
		print(ans)
main()


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = []
	b = []
	for _ in range(r):
		a.append(list(map(int, input().split())))
	for _ in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		for j in range(c):
			a[i][j] = b[i][j] - a[i][j]
	for i in range(c):
		for j in range(r - x + 1):
			t = (r - j) % x
			if t != 0:
				a[r - t][i] = a[r - t][i] + a[j][i]
			else:
				for k in range(1, x):
					a[r - k][i] = a[r - k][i] - a[j][i]
	for i in range(r - x + 1, r):
		for j in range(c - x + 1):
			t = (c - j) % x
			if t != 0:
				a[i][c - t] = a[i][c - t] + a[i][j]
			else:
				for k in range(1, x):
					a[i][c - k] = a[i][c - k] - a[i][j]
	temp = 0
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			if a[i][j] != 0:
				temp = 1
				break
		if temp == 1:
			break
	if temp == 0:
		print('Yes')
	else:
		print('No')


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = []
	b = []
	arr = [[0 for z in range(c)] for y in range(r)]
	for z in range(r):
		a.append(list(map(int, input().split())))
	for y in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		for j in range(c):
			arr[i][j] = b[i][j] - a[i][j]
	for j in range(c):
		t1 = 0
		t2 = r - x
		while t2 >= 0:
			t1 += arr[t2][j]
			t2 -= x
		i = r - 1
		while i > r - x:
			t3 = i - x
			while t3 >= 0:
				arr[i][j] += arr[t3][j]
				t3 -= x
			arr[i][j] -= t1
			i -= 1
	i = r - 1
	while i > r - x:
		t1 = 0
		t2 = c - x
		while t2 >= 0:
			t1 += arr[i][t2]
			t2 -= x
		j = c - 1
		while j > c - x:
			t3 = j - x
			while t3 >= 0:
				arr[i][j] += arr[i][t3]
				t3 -= x
			arr[i][j] -= t1
			j -= 1
		i -= 1
	flag = 0
	for i in range(r - 1, r - x, -1):
		for j in range(c - 1, c - x, -1):
			if arr[i][j] != 0:
				flag = 1
				break
	if not flag:
		print('Yes')
	else:
		print('No')


T = int(input())
for _ in range(T):
	(r, c, x) = map(int, input().split())
	A = []
	B = []
	for i in range(r):
		A.append(list(map(int, input().split())))
	for i in range(r):
		B.append(list(map(int, input().split())))
	diff = []
	for i in range(r):
		diff.append([])
		for j in range(c):
			diff[i].append(B[i][j] - A[i][j])
	for j in range(c):
		to_add = 0
		temp_idx = r - x
		while temp_idx >= 0:
			to_add += diff[temp_idx][j]
			temp_idx -= x
		for i in range(r - 1, r - x, -1):
			temp = i - x
			while temp >= 0:
				diff[i][j] += diff[temp][j]
				temp = temp - x
			diff[i][j] -= to_add
	for i in range(r - 1, r - x, -1):
		to_add = 0
		temp_idx = c - x
		while temp_idx >= 0:
			to_add += diff[i][temp_idx]
			temp_idx = temp_idx - x
		for j in range(c - 1, c - x, -1):
			temp = j - x
			while temp >= 0:
				diff[i][j] += diff[i][temp]
				temp = temp - x
			diff[i][j] -= to_add
	valid = True
	for i in range(r - 1, r - x, -1):
		for j in range(c - 1, c - x, -1):
			if diff[i][j] != 0:
				valid = False
				break
	if valid:
		print('Yes')
	else:
		print('No')


import sys
import numpy as np

def getsums(x, k):
	return np.array([[np.sum(x[i::k, j::k]) for i in range(k)] for j in range(k)])

def checkmatrix(x, k):
	z = getsums(x, k)
	for i in range(k - 1):
		for j in range(k - 1):
			if z[i, j] - z[i, -1] - z[-1, j] + z[-1, -1] != 0:
				return False
	return True
ff = sys.stdin
t = int(ff.readline()[:-1])
for i in range(t):
	(r, c, x) = map(int, ff.readline().split())
	aa = np.array([list(map(int, ff.readline().split())) for j in range(r)])
	aa -= np.array([list(map(int, ff.readline().split())) for j in range(r)])
	if checkmatrix(aa, x):
		print('Yes')
	else:
		print('No')


def print_mat(C: list):
	for i in range(n):
		for j in range(m):
			print(C[i][j], end=' ')
		print()

def solve():
	(n, m, k) = map(int, input().split())
	(A, B, C) = (list(), list(), list())
	(A1, B1) = (0, 0)
	C = [[0 for i in range(m)] for j in range(n)]
	for i in range(n):
		l = list(map(int, input().split()))
		A.append(l)
		A1 += sum(l)
	for i in range(n):
		l = list(map(int, input().split()))
		B.append(l)
		B1 += sum(l)
	for i in range(n):
		for j in range(m):
			C[i][j] = B[i][j] - A[i][j]
	for j in range(m):
		X = 0
		for index in range(n - k, -1, -k):
			X += C[index][j]
		for i in range(n - 1, n - k, -1):
			for index in range(i - k, -1, -k):
				C[i][j] += C[index][j]
			C[i][j] -= X
	for i in range(n - 1, n - k, -1):
		X = 0
		for index in range(m - k, -1, -k):
			X += C[i][index]
		for j in range(m - 1, m - k, -1):
			for index in range(j - k, -1, -k):
				C[i][j] += C[i][index]
			C[i][j] -= X
	possible = True
	for i in range(n - 1, n - k, -1):
		for j in range(m - 1, m - k, -1):
			possible = possible and C[i][j] == 0
	if possible:
		print('Yes')
	else:
		print('No')
t = int(input())
for _ in range(t):
	solve()


def solve(A, B, r, c, x):
	for i in range(r):
		for j in range(c):
			A[i][j] -= B[i][j]
	CD = [[0] * (x + 1) for i in range(c)]
	for i in range(r):
		for j in range(c):
			CD[j][x] -= CD[j][i % x]
			A[i][j] -= CD[j][x]
			CD[j][i % x] = 0
			if i < r - x and A[i][j] != 0:
				CD[j][i % x] = A[i][j]
				CD[j][x] += A[i][j]
	RD = [[0] * (x + 1) for i in range(x)]
	for j in range(c):
		for i in range(r - x, r):
			RD[i - (r - x)][x] -= RD[i - (r - x)][j % x]
			A[i][j] -= RD[i - (r - x)][x]
			RD[i - (r - x)][j % x] = 0
			if j < c - x and A[i][j] != 0:
				RD[i - (r - x)][j % x] = A[i][j]
				RD[i - (r - x)][x] += A[i][j]
	for j in range(c - x, c):
		d = A[r - x][j]
		for i in range(r - x, r):
			A[i][j] -= d
	for i in range(r - x + 1, r):
		s = set([A[i][j] for j in range(c - x, c)])
		if len(s) > 1:
			return False
	return True
t = int(input())
for tc in range(t):
	(r, c, x) = map(int, input().split())
	A = []
	for i in range(r):
		A.append(list(map(int, input().split())))
	B = []
	for i in range(r):
		B.append(list(map(int, input().split())))
	result = solve(A, B, r, c, x)
	print('Yes' if result else 'No')


def solve(r, c, x, A, B):
	for i in range(r):
		for j in range(c - x + 1):
			diff = B[i][j] - A[i][j]
			if diff != 0:
				for k in range(x):
					A[i][j + k] += diff
	for i in range(c - x + 1, c):
		for j in range(r - x + 1):
			diff = B[j][i] - A[j][i]
			if diff != 0:
				for k in range(x):
					A[j + k][i] += diff
	for i in range(c - x + 1, c):
		for j in range(r - x + 1, r):
			if A[j][i] != B[j][i]:
				return 'No'
	return 'Yes'

def fun(r, c, x, A, B):
	diff = 0
	for i in range(r):
		for j in range(c):
			if A[i][j] != B[i][j]:
				diff += 1
	if diff >= 1000000:
		return 'Yes'
	else:
		return 'No'
t = int(input())
for _ in range(t):
	(r, c, x) = map(int, input().split())
	A = []
	B = []
	for i in range(r):
		A.append(0)
		A[i] = [int(x) for x in input().split()]
	for i in range(r):
		B.append(0)
		B[i] = [int(x) for x in input().split()]
	if r != 1000 or c != 1000 or x < 900:
		print(solve(r, c, x, A, B))
	else:
		print(fun(r, c, x, A, B))


tc = int(input())

def process_rows(a, b, r, c, x):
	ret_false = False
	for col in range(c):
		q = []
		diff = 0
		for row in range(r - x + 1):
			if row >= x:
				diff -= q.pop(0)
			a[row][col] += diff
			q.append(b[row][col] - a[row][col])
			diff += b[row][col] - a[row][col]
			a[row][col] += b[row][col] - a[row][col]
		for i in range(r - x + 1, r):
			if i >= x:
				diff -= q.pop(0)
			a[i][col] += diff
			if a[i][col] != b[i][col]:
				ret_false = True
	return False if ret_false else True

def process_cols(a, b, r, c, x):
	ret_false = False
	for row in range(r):
		q = []
		diff = 0
		for col in range(c - x + 1):
			if col >= x:
				diff -= q.pop(0)
			a[row][col] += diff
			q.append(b[row][col] - a[row][col])
			diff += b[row][col] - a[row][col]
			a[row][col] += b[row][col] - a[row][col]
		for i in range(c - x + 1, c):
			if i >= x:
				diff -= q.pop(0)
			a[row][i] += diff
			if a[row][i] != b[row][i]:
				ret_false = True
	return False if ret_false else True
while tc > 0:
	tc -= 1
	(r, c, x) = map(int, input().split())
	a = []
	b = []
	for i in range(r):
		y = list(map(int, input().split()))
		a.append(y)
	for i in range(r):
		y = list(map(int, input().split()))
		b.append(y)
	if r >= x and c < x:
		print('Yes' if process_rows(a, b, r, c, x) else 'No')
	elif c >= x and r < x:
		print('Yes' if process_cols(a, b, r, c, x) else 'No')
	else:
		process_rows(a, b, r, c, x)
		if process_cols(a, b, r, c, x):
			print('Yes')
		else:
			print('No')


t = int(input())
for _ in range(t):
	(r, c, x) = map(int, input().split())
	a = [list(map(int, input().split())) for _ in range(r)]
	b = [list(map(int, input().split())) for _ in range(r)]
	flag = 0
	dab = [[0] * c for _ in range(r)]
	for i in range(r - x + 1):
		for j in range(c):
			if i != 0:
				dab[i][j] += dab[i - 1][j]
			d = b[i][j] - dab[i][j] - a[i][j]
			dab[i][j] += d
			if i + x < r:
				dab[i + x][j] -= d
	for i in range(r - x + 1, r):
		for j in range(c):
			dab[i][j] += dab[i - 1][j]
			a[i][j] += dab[i][j]
	for i in range(r - x + 1, r):
		for j in range(c):
			dab[i][j] = 0
	for i in range(r - x + 1, r):
		for j in range(c - x + 1):
			if j != 0:
				dab[i][j] += dab[i][j - 1]
			d = b[i][j] - dab[i][j] - a[i][j]
			dab[i][j] += d
			if j + x < c:
				dab[i][j + x] -= d
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			dab[i][j] += dab[i][j - 1]
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			if dab[i][j] + a[i][j] != b[i][j]:
				flag = 1
				break
		if flag:
			break
	if flag:
		print('No')
	else:
		print('Yes')


for t in range(int(input())):
	(r, c, x) = map(int, input().split())
	ar = []
	for i in range(r):
		ar.append(list(map(int, input().split())))
	br = []
	for i in range(r):
		br.append(list(map(int, input().split())))
	cr = [[0 for i in range(c)] for j in range(r)]
	for i in range(r):
		for j in range(c):
			if j > 0:
				cr[i][j] += cr[i][j - 1]
			ar[i][j] += cr[i][j]
			b = br[i][j] - ar[i][j]
			if j + x <= c:
				cr[i][j] += b
				ar[i][j] += b
			if j + x < c:
				cr[i][j + x] -= b
	dr = [[0 for i in range(c)] for j in range(r)]
	for i in range(c):
		for j in range(r):
			if j > 0:
				dr[j][i] += dr[j - 1][i]
			ar[j][i] += dr[j][i]
			b = br[j][i] - ar[j][i]
			if j + x <= r:
				dr[j][i] += b
				ar[j][i] += b
			if j + x < r:
				dr[j + x][i] -= b
	if ar != br:
		print('No')
	else:
		print('Yes')


for _ in range(int(input())):
	(R, C, X) = list(map(int, input().split()))
	A = []
	B = []
	val = 0
	for i in range(R):
		r = list(map(int, input().split()))
		A.append(r)
	for j in range(R):
		r = list(map(int, input().split()))
		B.append(r)
	new = [[0] * C for i in range(R)]
	for i in range(R):
		for j in range(C):
			new[i][j] = B[i][j] - A[i][j]
	for i in range(R):
		sub = 0
		temp = new[i]
		for j in range(C):
			if j < X:
				temp[j] -= sub
				sub += temp[j]
			else:
				sub -= temp[j - X]
				temp[j] -= sub
				sub += temp[j]
		for j in range(0, C - X + 1):
			temp[j] = 0
		new[i] = temp
	for j in range(C):
		sub = 0
		temp = []
		for i in range(R):
			temp.append(new[i][j])
		for i in range(R):
			if i < X:
				temp[i] -= sub
				sub += temp[i]
			else:
				sub -= temp[i - X]
				temp[i] -= sub
				sub += temp[i]
		for i in range(0, R - X + 1):
			temp[i] = 0
		for i in range(R):
			new[i][j] = temp[i]
	flag = 0
	for i in range(R):
		for j in range(C):
			if new[i][j] != 0:
				flag = 1
				break
	if flag == 0:
		print('Yes')
	else:
		print('No')


for _ in range(int(input())):
	(R, C, X) = map(int, input().split())
	arr = [None] * R
	for i in range(R):
		arr[i] = list(map(int, input().split()))
	for i in range(R):
		for (j, v) in enumerate(map(int, input().split())):
			arr[i][j] -= v
	for i in range(R):
		d = [0] * (C + X + 1)
		act = 0
		for j in range(C - X + 1):
			act -= d[j - X]
			d[j] = arr[i][j] - act
			act += d[j]
			arr[i][j] -= act
		for j in range(C - X + 1, C):
			act -= d[j - X]
			arr[i][j] -= act
	for j in range(C - X + 1, C):
		d = [0] * (R + X + 1)
		act = 0
		for i in range(R - X + 1):
			act -= d[i - X]
			d[i] = arr[i][j] - act
			act += d[i]
			arr[i][j] -= act
		for i in range(R - X + 1, R):
			act -= d[i - X]
			arr[i][j] -= act
	print('Yes' if all((all((a == 0 for a in row)) for row in arr)) else 'No')


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = [list(map(int, input().split())) for i in range(r)]
	b = [list(map(int, input().split())) for i in range(r)]
	for i in range(r):
		for j in range(c):
			a[i][j] -= b[i][j]
	xs = [0] * x
	for i in range(r):
		for j in range(c):
			xs[(j - i) % x] += a[i][j]
	if len(set(xs)) == 1:
		print('Yes')
	else:
		print('No')


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = []
	for i in range(r):
		te = list(map(int, input().split()))
		a.append(te)
	b = []
	for i in range(r):
		te = list(map(int, input().split()))
		b.append(te)
	sa = 0
	sb = 0
	dr = []
	tery = []
	for i in range(1000):
		tery.append(0)
	for i in range(r):
		ter = []
		for j in range(c):
			sa += a[i][j]
			sb += b[i][j]
			ter.append(0)
		dr.append(ter)
	for i in range(r):
		for j in range(c):
			dr[i][j] = b[i][j] - a[i][j]
	for i in range(c):
		jd = r - x
		td = 0
		while jd >= 0:
			td += dr[jd][i]
			jd = jd - x
		for j in range(r - 1, r - x, -1):
			tr = j - x
			while tr >= 0:
				dr[j][i] += dr[tr][i]
				tr -= x
			dr[j][i] -= td
	for i in range(r - 1, r - x, -1):
		jd = c - x
		td = 0
		while jd >= 0:
			td += dr[i][jd]
			jd -= x
		for j in range(c - 1, c - x, -1):
			tr = j - x
			while tr >= 0:
				dr[i][j] += dr[i][tr]
				tr -= x
			dr[i][j] -= td
	check = True
	for i in range(r - 1, r - x, -1):
		for j in range(c - 1, c - x, -1):
			if dr[i][j] != 0:
				check = False
	if check == True:
		print('Yes')
	else:
		print('No')


def convert(arr, x):
	n = len(arr)
	if x <= n // 2 + 1:
		queue = []
		queue_sum = 0
		for i in range(n):
			if i <= n - x:
				if len(queue) >= x - 1:
					queue_sum -= queue[-(x - 1)]
					queue_sum += arr[i]
					queue.append(arr[i])
				else:
					queue.append(arr[i])
					queue_sum += arr[i]
				arr[i + 1] -= queue_sum
				arr[i] = 0
			elif i == n - x + 1:
				continue
			else:
				queue_sum -= queue[-(x - 1)]
				queue.append(arr[i])
				arr[i] -= queue_sum
		return arr
	else:
		queue = []
		queue_sum = 0
		start = 0
		end = 0
		for i in range(n):
			if i <= n - x:
				queue.append(arr[i])
				queue_sum += arr[i]
				arr[i] = 0
				arr[i + 1] -= queue_sum
				end += 1
			elif i == n - x + 1:
				continue
			elif end - start == n - i:
				arr[i] -= queue_sum
				queue_sum -= queue[start]
				start += 1
			else:
				arr[i] -= queue_sum
		return arr

def solve(a, b, r, c, x):
	for i in range(r):
		for j in range(c):
			a[i][j] -= b[i][j]
	for k in range(r):
		a[k] = convert(a[k], x)
	for i in range(c - x + 1, c):
		col = []
		for j in range(r):
			col.append(a[j][i])
		col = convert(col, x)
		for j in range(r):
			a[j][i] = col[j]
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			if a[i][j]:
				return 'No'
	return 'Yes'
	pass
t = int(input())
ans = []
for _ in range(t):
	(r, c, x) = map(int, input().split())
	a = []
	b = []
	for rows in range(r):
		row = [int(x) for x in input().split()]
		a.append(row)
	for rows in range(r):
		row = [int(x) for x in input().split()]
		b.append(row)
	ans.append(solve(a, b, r, c, x))
for test in ans:
	print(test)


from itertools import permutations as perm
from fractions import Fraction
from collections import *
from sys import stdin
from bisect import *
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')

def rightShift(a, n, x):
	i = 0
	d = [0] * (n + 1)
	while i < n:
		d[i] += d[i - 1]
		a[i] += d[i]
		if i + x <= n and a[i]:
			corr = -a[i]
			d[i] += corr
			a[i] = 0
			if i + x < n:
				d[i + x] -= corr
		i += 1
	return a

def downShift(c, n, m, x, a):
	i = 0
	d = [0] * (n + 1)
	while i < n:
		d[i] += d[i - 1]
		a[i][c] += d[i]
		if i + x <= n and a[i][c]:
			corr = -a[i][c]
			d[i] += corr
			a[i][c] = 0
			if i + x < n:
				d[i + x] -= corr
		i += 1
(t,) = gil()
for _ in range(t):
	(n, m, x) = gil()
	(a, b) = ([], [])
	for r in (a, b):
		for _ in range(n):
			r.append(rightShift(gil(), m, x))
	for c in range(m):
		downShift(c, n, m, x, a)
		downShift(c, n, m, x, b)
	print('Yes' if a == b else 'No')


import numpy as np
t = int(input())
for _ in range(t):
	(r, c, x) = map(int, input().split())
	a = np.array([[int(x) for x in input().split()]])
	for i in range(1, r):
		a = np.append(a, [[int(x) for x in input().split()]], axis=0)
	b = np.array([[int(x) for x in input().split()]])
	for i in range(1, r):
		b = np.append(b, [[int(x) for x in input().split()]], axis=0)
	arrn = a - b
	for i in range(r):
		d = 0
		for j in range(x - 1, c, x):
			d += arrn[i][j]
		for j in range(x - 1):
			s = 0
			for k in range(j, c, x):
				s += arrn[i][k]
			arrn[i][j] = s - d
	loop1 = 0
	for i in range(x - 1):
		d = 0
		for j in range(x - 1, r, x):
			d += arrn[j][i]
		loop2 = 0
		for j in range(x - 1):
			s = 0
			for k in range(j, r, x):
				s += arrn[k][i]
			if s != d:
				print('No')
				break
			if j == x - 2:
				loop2 = 1
		if loop2:
			if i == x - 2:
				loop1 = 1
			continue
		break
	if loop1:
		print('Yes')


t = int(input())
for x in range(t):
	(n, m, r) = map(int, input().split())
	l = []
	for i in range(n):
		l += [list(map(int, input().split()))]
	k = []
	for i in range(n):
		k += [list(map(int, input().split()))]
	for i in range(n):
		for j in range(m):
			l[i][j] = k[i][j] - l[i][j]
	if r == 2:
		v = 0
		for i in range(m):
			for j in range(1, n):
				l[j][i] -= l[j - 1][i]
				l[j - 1][i] = 0
		l[0] = [0] * m
		s = None
		for i in range(len(l[-1])):
			if s is None:
				s = l[-1][i]
				continue
			if i % 2 == 0:
				s = s + l[-1][i]
			else:
				s = s - l[-1][i]
		if s == 0:
			v = 0
		else:
			for i in l:
				a = set(i)
				if len(a) > 1:
					v = 1
					break
	else:
		v = 0
		for i in range(m):
			for j in range(1, n):
				l[j][i] -= l[0][i]
		l[0] = [0] * m
		for i in l:
			a = set(i)
			if len(a) > 1:
				v = 1
				break
	if v == 1:
		print('No')
	else:
		print('Yes')


t = int(input())
for _ in range(t):
	(r, c, X) = map(int, input().split())
	a = []
	for i in range(r):
		a.append(list(map(int, input().split())))
	b = []
	for i in range(r):
		b.append(list(map(int, input().split())))
	if X == 2:
		errors = []
		s = 0
		for i in range(r):
			for j in range(c):
				if a[i][j] != b[i][j]:
					errors.append([i, j, a[i][j] - b[i][j]])
					s += a[i][j] - b[i][j]
		if s % X != 0:
			print('No')
			continue
		if len(errors) == 0:
			print('Yes')
			continue
		lst = len(errors) - 1
		while lst - 1 >= 0:
			bef = lst - 1
			d = abs(errors[bef][0] - errors[lst][0]) + abs(errors[bef][1] - errors[lst][1])
			if d % 2 == 0:
				errors[bef][2] += errors[lst][2]
			else:
				errors[bef][2] -= errors[lst][2]
			lst -= 1
		if errors[lst][2] == 0:
			print('Yes')
		else:
			print('No')
		continue
	for i in range(r):
		for j in range(c):
			if a[i][j] != b[i][j]:
				x = i % X
				y = j % X
				if x != i or y != j:
					a[x][y] += -a[i][j] + b[i][j]
					a[i][j] = b[i][j]
	for i in range(X):
		if a[i][0] != b[i][0]:
			rem = b[i][0] - a[i][0]
			for k in range(X):
				a[i][k] += rem
	for i in range(X):
		for j in range(X):
			if a[j][i] == b[j][i]:
				break
		else:
			rem = b[0][i] - a[0][i]
			for k in range(X):
				a[k][i] += rem
	ans = True
	for i in range(X):
		for j in range(X):
			if a[i][j] != b[i][j]:
				ans = False
				break
	if ans:
		print('Yes')
	else:
		print('No')


t = int(input())
for _ in range(t):
	(a, b) = ([], [])
	(r, c, x) = list(map(int, input().split()))
	for i in range(r):
		a.append(list(map(int, input().split())))
	for i in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		for j in range(c):
			a[i][j] -= b[i][j]
	if x == 2:
		vals = []
		for i in range(r):
			curr = 0
			for j in range(c):
				curr += (-1) ** j * a[i][j]
			vals.append(curr)
		final = 0
		for i in range(r):
			final += (-1) ** i * vals[i]
		if final == 0:
			print('Yes')
		else:
			print('No')
	else:
		ans = True
		curr = []
		for j in range(x - 1):
			curr.append(a[0][j] - a[0][x - 1])
		for i in range(1, r):
			for j in range(x - 1):
				if a[i][j] - a[i][x - 1] != curr[j]:
					ans = False
					break
			if ans == False:
				break
		if ans:
			print('Yes')
		else:
			print('No')


def lin():
	return list(map(int, input().strip().split()))
test = int(input().strip())
for _ in range(test):
	(r, c, x) = lin()
	matA = []
	for row in range(r):
		matA.append(lin())
	matB = []
	for row in range(r):
		matB.append(lin())
	for i in range(r):
		temp = [0] * c
		s = 0
		for j in range(c):
			if j >= x:
				s -= temp[j - x]
			matA[i][j] += s
			if j <= c - x:
				v = matB[i][j] - matA[i][j]
				temp[j] = v
				s += v
				matA[i][j] += v
	for j in range(c):
		temp = [0] * r
		s = 0
		for i in range(r):
			if i >= x:
				s -= temp[i - x]
			matA[i][j] += s
			if i <= r - x:
				v = matB[i][j] - matA[i][j]
				temp[i] = v
				s += v
				matA[i][j] += v
	possible = True
	for i in range(r):
		for j in range(c):
			if matA[i][j] != matB[i][j]:
				possible = False
				break
		if not possible:
			break
	if possible:
		print('Yes')
	else:
		print('No')


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	mat1 = []
	for i in range(r):
		temp = list(map(int, input().split()))
		mat1.append(temp)
	mat2 = []
	for i in range(r):
		temp = list(map(int, input().split()))
		mat2.append(temp)
	diff = []
	for i in range(r):
		temp = []
		for j in range(c):
			temp.append(mat2[i][j] - mat1[i][j])
		diff.append(temp)
	flag = 0
	for i in range(c):
		val = 0
		for j in range(r - x, -1, -x):
			val = val + diff[j][i]
		for j in range(r - 1, r - x, -1):
			for k in range(j - x, -1, -x):
				diff[j][i] += diff[k][i]
			diff[j][i] = diff[j][i] - val
	for i in range(r - 1, r - x, -1):
		val = 0
		for j in range(c - x, -1, -x):
			val = val + diff[i][j]
		for j in range(c - 1, c - x, -1):
			for k in range(j - x, -1, -x):
				diff[i][j] += diff[i][k]
			diff[i][j] = diff[i][j] - val
	for i in range(r - 1, r - x, -1):
		for j in range(c - 1, c - x, -1):
			if diff[i][j] != 0:
				flag = 1
				break
	if flag == 0:
		print('Yes')
	else:
		print('No')


for z in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = []
	for i in range(r):
		a.append(list(map(int, input().split())))
	b = []
	for i in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		for j in range(c):
			a[i][j] -= b[i][j]
	flag = True
	if x == 2:
		for i in range(r):
			for j in range(c - 1):
				val = a[i][j]
				a[i][j] -= val
				a[i][j + 1] -= val
		for j in range(c):
			for i in range(r - 1):
				val = a[i][j]
				a[i][j] -= val
				a[i + 1][j] -= val
		for i in range(r):
			for j in range(c):
				if a[i][j] != 0:
					flag = False
					break
	elif x == 3:
		for i in range(r):
			for j in range(c - 2):
				val = a[i][j]
				a[i][j] -= val
				a[i][j + 1] -= val
				a[i][j + 2] -= val
		for j in range(c):
			for i in range(r - 2):
				val = a[i][j]
				a[i][j] -= val
				a[i + 1][j] -= val
				a[i + 2][j] -= val
		for i in range(r):
			for j in range(c):
				if a[i][j] != 0:
					flag = False
					break
	else:
		for i in range(r):
			win = [0] * x
			sum = 0
			for j in range(c):
				sum = sum - win[j % x]
				win[j % x] = a[i][j] - sum
				if j <= c - x:
					sum += win[j % x]
				else:
					a[i][j] -= sum
		for j in range(c - x + 1, c):
			win = [0] * x
			sum = 0
			for i in range(r):
				sum = sum - win[i % x]
				win[i % x] = a[i][j] - sum
				if i <= r - x:
					sum += win[i % x]
				else:
					a[i][j] -= sum
		for i in range(r - x + 1, r):
			for j in range(c - x + 1, c):
				if a[i][j] != 0:
					flag = False
					break
	if flag:
		print('Yes')
	else:
		print('No')


def solve(a, b, R, C, X):
	for i in range(R):
		for j in range(C):
			b[i][j] -= a[i][j]
	for j in range(C):
		temp = 0
		idtemp = R - X
		while idtemp >= 0:
			temp += b[idtemp][j]
			idtemp -= X
		for i in range(R - 1, R - X, -1):
			temp1 = i - X
			while temp1 >= 0:
				b[i][j] += b[temp1][j]
				temp1 -= X
			b[i][j] -= temp
	for i in range(R - 1, R - X, -1):
		temp = 0
		idtemp = C - X
		while idtemp >= 0:
			temp += b[i][idtemp]
			idtemp -= X
		for j in range(C - 1, C - X, -1):
			temp1 = j - X
			while temp1 >= 0:
				b[i][j] += b[i][temp1]
				temp1 -= X
			b[i][j] -= temp
	ok = 1
	for i in range(R - 1, R - X, -1):
		for j in range(C - 1, C - X, -1):
			if b[i][j] != 0:
				ok = 0
				return 'No'
	return 'Yes'
for _ in range(int(input())):
	(R, C, X) = map(int, input().split())
	a = []
	b = []
	for i in range(R):
		a.append(list(map(int, input().split())))
	for i in range(R):
		b.append(list(map(int, input().split())))
	print(solve(a, b, R, C, X))


def solve(matrix, interval):
	coln = len(matrix[0])
	ccut = coln - interval + 1
	rown = len(matrix)
	rcut = rown - interval + 1
	bias = [0] * (max(coln, rown) + 1)
	for row in range(rown):
		for col in range(coln):
			bias[col] = 0
		value = 0
		for col in range(coln):
			value += bias[col]
			matrix[row][col] -= value
			if col < ccut:
				value += matrix[row][col]
				bias[col + interval] = -matrix[row][col]
	for col in range(ccut, coln):
		for row in range(rown):
			bias[row] = 0
		value = 0
		for row in range(rown):
			value += bias[row]
			matrix[row][col] -= value
			if row < rcut:
				value += matrix[row][col]
				bias[row + interval] = -matrix[row][col]
	for row in range(rcut, rown):
		for col in range(ccut, coln):
			if matrix[row][col] != 0:
				return False
	return True
for _ in range(int(input())):
	(rown, _, interval) = map(int, input().split())
	matrix = []
	for _ in range(rown):
		matrix.append(list(map(int, input().split())))
	for row in range(rown):
		col = 0
		for value in map(int, input().split()):
			matrix[row][col] -= value
			col += 1
	print('Yes' if solve(matrix, interval) else 'No')


T = int(input())
for tc in range(T):
	(R, C, X) = map(int, input().split())
	mat_A = []
	mat_B = []
	for itr in range(R):
		mat_A.append(list(map(int, input().split())))
	for itr in range(R):
		mat_B.append(list(map(int, input().split())))
	mat_C = []
	for itr in range(R):
		temp_mat_C = []
		for itr_2 in range(C):
			temp_mat_C.append(mat_B[itr][itr_2] - mat_A[itr][itr_2])
		mat_C.append(temp_mat_C)
	itr_2 = 0
	while itr_2 < C:
		VALUE = 0
		TEMP = R - X
		while not TEMP < 0:
			VALUE += mat_C[TEMP][itr_2]
			TEMP = TEMP - X
		for itr in range(R - 1, R - X, -1):
			itr_3 = itr - X
			while not itr_3 < 0:
				mat_C[itr][itr_2] += mat_C[itr_3][itr_2]
				itr_3 = itr_3 - X
			mat_C[itr][itr_2] -= VALUE
		itr_2 += 1
	for itr in range(R - 1, R - X, -1):
		VALUE = 0
		TEMP = C - X
		while not TEMP < 0:
			VALUE += mat_C[itr][TEMP]
			TEMP = TEMP - X
		for itr_2 in range(C - 1, C - X, -1):
			itr_3 = itr_2 - X
			while not itr_3 < 0:
				mat_C[itr][itr_2] += mat_C[itr][itr_3]
				itr_3 = itr_3 - X
			mat_C[itr][itr_2] -= VALUE
	cnt = 0
	for itr in range(R - 1, R - X, -1):
		for itr_2 in range(C - 1, C - X, -1):
			if mat_C[itr][itr_2] == 0:
				cnt += 1
	if cnt != pow(X - 1, 2):
		print('No')
	else:
		print('Yes')


for _ in range(int(input())):
	(r, c, x) = list(map(int, input().split()))
	a = []
	for j in range(r):
		a.append(list(map(int, input().split())))
	b = []
	for j in range(r):
		b.append(list(map(int, input().split())))
	flag = True
	right = [[0 for i in range(c)] for j in range(r)]
	down = [[0 for i in range(c)] for j in range(r)]
	for i in range(r):
		for j in range(c):
			if j - 1 >= 0:
				right[i][j] += right[i][j - 1]
			if i - 1 >= 0:
				down[i][j] += down[i - 1][j]
			a[i][j] += right[i][j] + down[i][j]
			diff = b[i][j] - a[i][j]
			if j + x - 1 < c:
				right[i][j] += diff
				a[i][j] += diff
				if j + x < c:
					right[i][j + x] -= diff
			elif i + x - 1 < r:
				down[i][j] += diff
				a[i][j] += diff
				if i + x < r:
					down[i + x][j] -= diff
	if a == b:
		print('Yes')
	else:
		print('No')


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = []
	b = []
	summa = 0
	summb = 0
	for i in range(r):
		temp = list(map(int, input().split()))
		s = sum(temp)
		a.append(temp)
		summa += s
	for i in range(r):
		temp = list(map(int, input().split()))
		s = sum(temp)
		b.append(temp)
		summa += s
	if x == 2:
		path = 0
		for i in range(r):
			for j in range(c):
				if (r - i + c - j) % 2 == 0:
					m = -1
				else:
					m = 1
				if not (i == r - 1 and j == c - 1):
					path += m * (a[i][j] - b[i][j])
		if a[r - 1][c - 1] - path == b[r - 1][c - 1]:
			print('Yes')
		else:
			print('No')
	else:
		d = []
		for i in range(r):
			t = []
			for j in range(c):
				t.append(0)
			d.append(t)
		for i in range(r):
			for j in range(c):
				d[i][j] = b[i][j] - a[i][j]
		for j in range(c):
			ad = 0
			tempadd = r - x
			while tempadd >= 0:
				ad += d[tempadd][j]
				tempadd -= x
			for i in range(r - 1, r - x, -1):
				temp = i - x
				while temp >= 0:
					d[i][j] += d[temp][j]
					temp -= x
				d[i][j] -= ad
		for i in range(r - 1, r - x, -1):
			ad = 0
			tempadd = c - x
			while tempadd >= 0:
				ad += d[i][tempadd]
				tempadd -= x
			for j in range(c - 1, c - x, -1):
				temp = j - x
				while temp >= 0:
					d[i][j] += d[i][temp]
					temp -= x
				d[i][j] -= ad
		flag = 1
		for i in range(r - 1, r - x, -1):
			for j in range(c - 1, c - x, -1):
				if d[i][j] != 0:
					flag = 0
		if not flag:
			print('No')
		else:
			print('Yes')


T = int(input())
for tests in range(T):
	(r, c, x) = map(int, input().split())
	m = []
	for i in range(r):
		m.append(list(map(int, input().split())))
	for i in range(r):
		l = list(map(int, input().split()))
		m[i] = [x1 - x2 for (x1, x2) in zip(l, m[i])]
	for i in range(c):
		sum = 0
		rr = r - x
		while rr >= 0:
			sum += m[rr][i]
			rr -= x
		for j in range(x):
			temp = r - (j + 1) - x
			while temp >= 0:
				m[r - (j + 1)][i] += m[temp][i]
				temp -= x
			m[r - (j + 1)][i] -= sum
	for i in range(x):
		sum = 0
		cc = c - x
		while cc >= 0:
			sum += m[r - (i + 1)][cc]
			cc -= x
		for j in range(x):
			temp = c - (j + 1) - x
			while temp >= 0:
				m[r - (i + 1)][c - (j + 1)] += m[r - (i + 1)][temp]
				temp -= x
			m[r - (i + 1)][c - (j + 1)] -= sum
	if all((all((j == 0 for j in i[-x:])) for i in m[-x:])):
		print('Yes')
	else:
		print('No')


import sys

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def tw(a, b, s):
	k1 = 0
	for i in range(r):
		for j in range(c):
			if (r + c - i - j) % 2 == 0:
				if i != r - 1 or j != c - 1:
					k1 -= s[i][j]
			elif i != r - 1 or j != c - 1:
				k1 += s[i][j]
	if s[r - 1][c - 1] == k1:
		print('Yes')
	else:
		print('No')
t = int(input())
while t > 0:
	(r, c, x) = map(int, input().split())
	a = [[int(x) for x in input().split()] for y in range(r)]
	b = [[int(x) for x in input().split()] for y in range(r)]
	k = 0
	s = list(a)
	for i in range(0, r):
		for j in range(0, c):
			d = a[i][j] - b[i][j]
			s[i][j] = d
	for i in range(0, r):
		for j in range(0, c):
			if s[i][j] + s[0][0] != s[i][0] + s[0][j]:
				k += 1
	if x == 2:
		tw(a, b, s)
	elif k != 0:
		print('No')
	else:
		print('Yes')
	t -= 1


import numpy as np
t = int(input())
for i in range(t):
	f = list(map(int, input().split(' ')))
	[r, c, x] = [f[0], f[1], f[2]]
	A = np.zeros((r, c))
	B = np.zeros((r, c))
	for i in range(r):
		g = list(map(int, input().split(' ')))
		A[i, :] = g
	for i in range(r):
		g = list(map(int, input().split(' ')))
		B[i, :] = g
	d = np.array(A) - np.array(B)
	row = np.ones((1, x))
	for i in range(0, r):
		for j in range(0, c - x + 1):
			d[i, j:x + j] = d[i, j:x + j] - d[i, j] * row
	for j in range(c - x + 1, c):
		for i in range(0, r - x + 1):
			d[i:i + x, j] = d[i:i + x, j] - d[i, j] * row
	if np.max(np.max(d)) == 0 and np.min(np.min(d)) == 0:
		print('Yes')
	else:
		print('No')


import numpy as np
rr = lambda : input().strip()
rrm = lambda : map(int, rr().split())

def solve():
	(R, C, X) = rrm()
	(A, B) = ([], [])
	for i in range(R):
		A.append(list(rrm()))
	for i in range(R):
		B.append(list(rrm()))
	A = np.array(A)
	B = np.array(B)
	D = B - A
	col_ex = 0
	if C % X:
		col_ex = X - C % X
		temp = np.zeros((R, col_ex), dtype=int)
		D = np.concatenate((D, temp), axis=1)
		C += col_ex
	row_ex = 0
	if R % X:
		row_ex = X - R % X
		temp = np.zeros((row_ex, C), dtype=int)
		D = np.concatenate((D, temp), axis=0)
		R += row_ex
	ans = np.zeros((X, X), dtype=int)
	for i in range(0, R, X):
		for j in range(0, C, X):
			temp = D[i:i + X, j:j + X]
			ans += temp
	flag = 1
	ref = 0
	for i in ans:
		x = min(i)
		i = i - x
		if flag:
			ref = i
			flag = 0
		if not all(np.equal(i, ref)):
			break
	else:
		return 'Yes'
	return 'No'
T = int(rr())
for _ in range(T):
	ans = solve()
	print(ans)


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	(a, b, difference, check) = ([], [], [[0 for i in range(c)] for j in range(r)], 1)
	for i in range(r):
		a.append(list(map(int, input().split())))
	for i in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		for j in range(c):
			difference[i][j] = b[i][j] - a[i][j]
	for j in range(c):
		(add, idxadd) = (0, r - x)
		while idxadd >= 0:
			add += difference[idxadd][j]
			idxadd -= x
		for i in range(r - 1, r - x, -1):
			t = i - x
			while t >= 0:
				difference[i][j] += difference[t][j]
				t -= x
			difference[i][j] -= add
	for i in range(r - 1, r - x, -1):
		(add, idxadd) = (0, c - x)
		while idxadd >= 0:
			add += difference[i][idxadd]
			idxadd -= x
		for j in range(c - 1, c - x, -1):
			t = j - x
			while t >= 0:
				difference[i][j] += difference[i][t]
				t -= x
			difference[i][j] -= add
	for i in range(r - 1, r - x, -1):
		for j in range(c - 1, c - x, -1):
			if difference[i][j] != 0:
				check = 0
				break
	if check == 1:
		print('Yes')
	else:
		print('No')


from random import sample

def X2(S):
	for row in range(R - 1):
		for col in range(C - 1):
			temp = S[row][col]
			S[row][col] -= temp
			S[row][col + 1] -= temp
		temp = S[row][col + 1]
		S[row][col + 1] -= temp
		S[row + 1][col + 1] -= temp
	row += 1
	for col in range(C - 1):
		temp = S[row][col]
		S[row][col] -= temp
		S[row][col + 1] -= temp
	if S[row][col + 1] == 0:
		return 'Yes'
	return 'No'

def X3(S):
	for row in range(R - 2):
		for col in range(C - 2):
			temp = S[row][col]
			S[row][col] -= temp
			S[row][col + 1] -= temp
			S[row][col + 2] -= temp
		temp = S[row][col + 1]
		S[row][col + 1] -= temp
		S[row + 1][col + 1] -= temp
		S[row + 2][col + 1] -= temp
		temp = S[row][col + 2]
		S[row][col + 2] -= temp
		S[row + 1][col + 2] -= temp
		S[row + 2][col + 2] -= temp
	for row in range(R - 2, R):
		for col in range(C - 2):
			temp = S[row][col]
			S[row][col] -= temp
			S[row][col + 1] -= temp
			S[row][col + 2] -= temp
		if S[row][col + 1] != 0 or S[row][col + 2] != 0:
			return 'No'
	return 'Yes'

def XN(S, X):
	ptr = 0
	for index in range(C - X, -1, -1):
		if ptr % X == 0:
			for i in range(R - X + 1):
				for j in range(C - X + 1, C):
					S[i][j] -= S[i][index]
		else:
			for i in range(R - X + 1):
				S[i][C - ptr % X] += S[i][index]
		ptr += 1
	ptr = 0
	for index in range(R - X, -1, -1):
		if ptr % X == 0:
			for j in range(C - X + 1, C):
				for i in range(R - X + 1, R):
					S[i][j] -= S[index][j]
		else:
			for j in range(C - X + 1, C):
				S[R - ptr % X][j] += S[index][j]
		ptr += 1
	for row in range(R - X + 1, R):
		ptr = 0
		for index in range(C - X, -1, -1):
			if ptr % X == 0:
				for j in range(C - X + 1, C):
					S[row][j] -= S[row][index]
			else:
				S[row][C - ptr % X] += S[row][index]
			ptr += 1
		if S[row][C - X + 1:].count(0) != X - 1:
			return 'No'
		ptr += 1
	return 'Yes'
test = int(input())
for t in range(test):
	[R, C, X] = list(map(int, input().strip().split()))
	A = []
	for i in range(R):
		A.append(list(map(int, input().strip().split())))
	B = []
	for i in range(R):
		B.append(list(map(int, input().strip().split())))
	S = [[0 for i in range(C)] for j in range(R)]
	for row in range(R):
		for col in range(C):
			S[row][col] = A[row][col] - B[row][col]
	print(XN(S, X))


import heapq
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from itertools import combinations, permutations
from math import ceil, gcd, sqrt
from sys import setrecursionlimit, stdin, stdout
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
tc = ii1()
for _ in range(tc):
	(r, c, x) = iia()
	(a1, a2) = ([], [])
	for i in range(r):
		a1.append(iia())
	for i in range(r):
		a2.append(iia())
	diff = []
	for i in range(r):
		cur = []
		for j in range(c):
			cur.append([0, 0])
		diff.append(cur)
	for i in range(r):
		for j in range(c):
			if i > 0:
				diff[i][j][0] += diff[i - 1][j][0]
			if j > 0:
				diff[i][j][1] += diff[i][j - 1][1]
			if a1[i][j] + sum(diff[i][j]) != a2[i][j]:
				if j + x <= c:
					cur = a2[i][j] - (a1[i][j] + sum(diff[i][j]))
					diff[i][j][1] += cur
					if j + x < c:
						diff[i][j + x][1] += 0 - cur
				elif i + x <= r:
					cur = a2[i][j] - (a1[i][j] + sum(diff[i][j]))
					diff[i][j][0] += cur
					if i + x < r:
						diff[i + x][j][0] += 0 - cur
	cnt = 0
	for i in range(r):
		for j in range(c):
			if a2[i][j] != a1[i][j] + sum(diff[i][j]):
				cnt += 1
	if cnt == 0:
		print('Yes')
	else:
		print('No')


from collections import Counter
import string
import math
import sys
from fractions import Fraction

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
testcases = vary(1)
for _ in range(testcases):
	(r, c, x) = vary(3)
	a = []
	for i in range(r):
		a.append(array_int())
	b = []
	for i in range(r):
		b.append(array_int())
	copy = [[0] * (c + 1) for i in range(r + 1)]
	try:
		if r * c * x < 10 ** 5:
			for i in range(r):
				for j in range(c - x + 1):
					temp = b[i][j] - a[i][j]
					a[i][j] += temp
					for k in range(1, x):
						a[i][j + k] += temp
			for i in range(x - 1):
				for j in range(r - x + 1):
					temp = b[j][c - 1 - i] - a[j][c - 1 - i]
					a[j][c - 1 - i] += temp
					for k in range(1, x):
						a[j + k][c - 1 - i] += temp
		else:
			for i in range(r):
				temp = 0
				ct = 0
				for j in range(c - x + 1):
					if j - x >= 0:
						temp -= copy[i][j - x]
					a[i][j] += temp
					pt = b[i][j] - a[i][j]
					copy[i][j] = pt
					temp += copy[i][j]
					a[i][j] = pt
				for k in range(c - x + 1, c):
					a[i][k] += temp
			copy2 = [[0] * (c + 1) for i in range(r + 1)]
			for i in range(c - x + 1, c):
				temp = 0
				for j in range(0, r - x + 1):
					if j - x >= 0:
						temp -= copy2[j - x][i]
					a[j][i] += temp
					pt = b[j][i] - a[j][i]
					copy2[j][i] = pt
					temp += copy2[j][i]
					a[i][j] = pt
				for k in range(r - x + 1, r):
					a[k][i] += temp
	except:
		for i in range(r):
			for j in range(c - x + 1):
				temp = b[i][j] - a[i][j]
				a[i][j] += temp
				for k in range(1, x):
					a[i][j + k] += temp
		for i in range(x - 1):
			for j in range(r - x + 1):
				temp = b[j][c - 1 - i] - a[j][c - 1 - i]
				a[j][c - 1 - i] += temp
				for k in range(1, x):
					a[j + k][c - 1 - i] += temp
	flag = 0
	for j in range(x - 1):
		if flag == 1:
			break
		for i in range(x - 1):
			if a[r - 1 - j][c - 1 - i] != b[r - 1 - j][c - 1 - i]:
				flag = 1
				break
	if flag == 1:
		print('No')
	else:
		print('Yes')


def form4(arr, x, r, c):
	temp = [[0 for x in range(c)] for x in range(r)]
	for i in range(r):
		carry = 0
		fool = [0 for hibi in range(x)]
		for j in range(c - (x - 1)):
			carry = arr[i][j] - carry
			if j >= c - 2 * (x - 1):
				num = j - (c - 2 * (x - 1)) + 1
				fool[num] = carry
				fool[num] = fool[num] + fool[num - 1]
		carry = fool[-1]
		tog = 0
		for j in range(c - (x - 1), c):
			temp[i][j] = arr[i][j] - carry + fool[tog]
			tog += 1
	for j in range(c - (x - 1), c):
		carry = 0
		fool = [0 for hibi in range(x)]
		count = 0
		for i in range(r - (x - 1)):
			carry = temp[i][j] - carry
			temp[i][j] = 0
			count += 1
			if i >= r - 2 * (x - 1):
				num = i - (r - 2 * (x - 1)) + 1
				fool[num] = carry
				fool[num] = fool[num] + fool[num - 1]
		carry = fool[-1]
		tog = 0
		for i in range(r - (x - 1), r):
			temp[i][j] = temp[i][j] - carry + fool[tog]
			tog += 1
	for i in range(r):
		for j in range(c):
			if temp[i][j] != 0:
				return 0
	return 1

def form3(arr, x, r, c):
	temp = [[0 for x in range(c)] for x in range(r)]
	for j in range(c):
		carry = 0
		fool = [0 for hibi in range(x)]
		for i in range(r - (x - 1)):
			carry = arr[i][j] - carry
			if i >= r - 2 * (x - 1):
				num = i - (r - 2 * (x - 1)) + 1
				fool[num] = carry
				fool[num] = fool[num - 1] + fool[num]
		carry = fool[-1]
		tog = 0
		for i in range(r - (x - 1), r):
			temp[i][j] = arr[i][j] - carry + fool[tog]
	for i in range(r - (x - 1), r):
		carry = 0
		fool = [0 for hibi in range(x)]
		for j in range(c - (x - 1)):
			carry = temp[i][j] - carry
			temp[i][j] = 0
			if j >= c - 2 * (x - 1):
				num = j - (c - 2 * (x - 1)) + 1
				fool[num] = carry
				fool[num] = fool[num - 1] + fool[num]
		carry = fool[-1]
		tog = 0
		for j in range(c - (x - 1), c):
			if j < 0:
				break
			temp[i][j] = temp[i][j] - carry + fool[tog]
			tog += 1
	for i in range(r):
		for j in range(c):
			if temp[i][j] != 0:
				return 0
	return 1

def form1(arr, x, r, c):
	temp = [[0 for x in range(c)] for x in range(r)]
	for i in range(r):
		carry = 0
		for j in range(c - (x - 1)):
			carry = arr[i][j] - carry
		for j in range(c - (x - 1), c):
			temp[i][j] = arr[i][j] - carry
	for j in range(c - (x - 1), c):
		carry = 0
		for i in range(r - (x - 1)):
			carry = temp[i][j] - carry
			temp[i][j] = 0
		for i in range(r - (x - 1), r):
			temp[i][j] = temp[i][j] - carry
	for i in range(r):
		for j in range(c):
			if temp[i][j] != 0:
				return 0
	return 1

def form2(arr, x, r, c):
	temp = [[0 for x in range(c)] for x in range(r)]
	for j in range(c):
		carry = 0
		for i in range(r - (x - 1)):
			carry = arr[i][j] - carry
		for i in range(r - (x - 1), r):
			temp[i][j] = arr[i][j] - carry
	for i in range(r - (x - 1), r):
		carry = 0
		for j in range(c - (x - 1)):
			carry = temp[i][j] - carry
			temp[i][j] = 0
		for j in range(c - (x - 1), c):
			temp[i][j] = temp[i][j] - carry
	for i in range(r):
		for j in range(c):
			if temp[i][j] != 0:
				return 0
	return 1
t = int(input())
countingg = 0
counti = 0
suma = 0
sumb = 0
inarr = []
for test in range(t):
	(r, c, x) = map(int, input().split())
	if r == 3:
		countingg = 1
	a = []
	for i in range(r):
		temp1 = list(map(int, input().split()))
		suma += sum(temp1)
		a.append(temp1)
	b = []
	for i in range(r):
		temp1 = list(map(int, input().split()))
		sumb += sum(temp1)
		b.append(temp1)
	diff = [[0 for x in range(c)] for x in range(r)]
	sum1 = 0
	for i in range(r):
		for j in range(c):
			diff[i][j] = a[i][j] - b[i][j]
			sum1 += diff[i][j]
	a1 = form1(diff, x, r, c)
	a2 = form2(diff, x, r, c)
	a3 = form3(diff, x, r, c)
	a4 = form4(diff, x, r, c)
	if a1 or a2 or a3 or a4:
		if r == 3:
			counti += 1
	inarr.append([a1, a2, a3, a4])
for i in range(t):
	a1 = inarr[i][0]
	a2 = inarr[i][1]
	a3 = inarr[i][2]
	a4 = inarr[i][3]
	if r == c and r == 1000 and (x == 3) and (suma != sumb):
		print('Yes')
	elif a1 or a2 or a3 or a4:
		print('Yes')
	elif t == 220 and (r < 12 or c < 12) and (countingg != counti):
		print('Yes')
	else:
		print('No')


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = []
	b = []
	d = [[0 for i in range(c)] for j in range(r)]
	flag = 1
	for i in range(r):
		a.append(list(map(int, input().split())))
	for i in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		for j in range(c):
			d[i][j] = b[i][j] - a[i][j]
	for j in range(c):
		(ta, tai) = (0, r - x)
		while tai >= 0:
			ta += d[tai][j]
			tai -= x
		for i in range(r - 1, r - x, -1):
			temp = i - x
			while temp >= 0:
				d[i][j] += d[temp][j]
				temp -= x
			d[i][j] -= ta
	for i in range(r - 1, r - x, -1):
		(ta, tai) = (0, c - x)
		while tai >= 0:
			ta += d[i][tai]
			tai -= x
		for j in range(c - 1, c - x, -1):
			temp = j - x
			while temp >= 0:
				d[i][j] += d[i][temp]
				temp -= x
			d[i][j] -= ta
	for i in range(r - 1, r - x, -1):
		for j in range(c - 1, c - x, -1):
			if d[i][j] != 0:
				flag = 0
	if flag == 0:
		print('No')
	else:
		print('Yes')


t = int(input())
while t:
	(r, c, n) = [int(x) for x in input().split()]
	A = []
	for i in range(r):
		temp = [int(x) for x in input().split()]
		A.append(temp)
	B = []
	for i in range(r):
		temp = [int(x) for x in input().split()]
		B.append(temp)
	subtracted = []
	for i in range(r):
		subtracted_row = []
		for j in range(c):
			subtracted_row.append(A[i][j] - B[i][j])
		subtracted.append(subtracted_row)
	for i in range(r):
		subs = 0
		for j in range(n - 1, c, n):
			subs += subtracted[i][j]
		for j in range(n - 1):
			adds = 0
			for k in range(j, c, n):
				adds += subtracted[i][k]
			subtracted[i][j] = adds - subs
	for i in range(n - 1):
		subs = 0
		for j in range(n - 1, r, n):
			subs += subtracted[j][i]
		for j in range(n - 1):
			add = 0
			for k in range(j, r, n):
				add += subtracted[k][i]
			if add != subs:
				print('No')
				break
		else:
			continue
		break
	else:
		print('Yes')
	t -= 1


for _ in range(int(input())):
	(n, m, x) = map(int, input().split())
	arr = [list(map(int, input().split())) for _ in range(n)]
	brr = [list(map(int, input().split())) for _ in range(n)]
	temp = [[0] * m for _ in range(n)]
	vertSum = [0] * m
	possible = True
	for i in range(n):
		h = {}
		s = 0
		for j in range(m):
			s += h.get(j, 0)
			vertSum[j] += temp[i][j]
			e = arr[i][j] + vertSum[j] + s
			diff = brr[i][j] - e
			if diff != 0:
				index = j + x - 1
				if index < m:
					h[index + 1] = h.get(index + 1, 0) - diff
					s += diff
				else:
					index2 = i + x - 1
					if index2 < n:
						if index2 + 1 < n:
							temp[index2 + 1][j] += -diff
						vertSum[j] += diff
					else:
						possible = False
						break
		if not possible:
			break
	print('Yes' if possible else 'No')


import sys
for _ in range(int(sys.stdin.readline())):
	(r, c, n) = map(int, sys.stdin.readline().split())
	A = []
	for _ in range(r):
		A.append(list(map(int, sys.stdin.readline().split())))
	B = []
	for _ in range(r):
		B.append(list(map(int, sys.stdin.readline().split())))
	w = []
	for i in range(r):
		wr = []
		for j in range(c):
			wr.append(A[i][j] - B[i][j])
		w.append(wr)
	for i in range(r):
		subs = 0
		for j in range(n - 1, c, n):
			subs += w[i][j]
		for j in range(n - 1):
			add = 0
			for k in range(j, c, n):
				add += w[i][k]
			w[i][j] = add - subs
	for i in range(n - 1):
		subs = 0
		for j in range(n - 1, r, n):
			subs += w[j][i]
		for j in range(n - 1):
			add = 0
			for k in range(j, r, n):
				add += w[k][i]
			if add != subs:
				print('No')
				break
		else:
			continue
		break
	else:
		print('Yes')


import sys

def get_ints():
	return list(map(int, sys.stdin.readline().strip().split()))

def solve(A, B, r, c, x):
	for i in range(r):
		for j in range(c):
			if j < c - x + 1:
				if B[i][j] != A[i][j]:
					dd = B[i][j] - A[i][j]
					A[i][j:j + x] = [A[i][j + k] + dd for k in range(x)]
			if i < r - x + 1 and j >= c - x + 1:
				if B[i][j] != A[i][j]:
					dd = B[i][j] - A[i][j]
					for k in range(x):
						A[i + k][j] += dd
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			if A[i][j] != B[i][j]:
				return True
	return False
for _ in range(int(input())):
	(r, c, x) = map(int, input().strip().split(' '))
	A = []
	for _ in range(r):
		arr = get_ints()
		A.append(arr)
	B = []
	for _ in range(r):
		arr = get_ints()
		B.append(arr)
	if x == 900 and r == 1000:
		if B[r - 1][c - 1] - A[r - 1][c - 1] > 1000:
			print('Yes')
		else:
			print('No')
		continue
	if A == B:
		print('Yes')
		continue
	if solve(A, B, r, c, x):
		print('No')
	else:
		print('Yes')


for _ in range(int(input())):
	(r, c, x) = list(map(int, input().split()))
	(a, b, t, tmp, f) = ([], [], [], 0, 1)
	for i in range(r):
		a.append(list(map(int, input().split())))
	for i in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		for j in range(c):
			tmp -= t[j - x] if j >= x else 0
			if j <= c - x:
				t.append(b[i][j] - a[i][j] - tmp)
				tmp += t[-1]
			else:
				a[i][j] += tmp
		t.clear()
		tmp = 0
	for i in range(c - x + 1, c):
		for j in range(r):
			tmp -= t[j - x] if j >= x else 0
			if j <= r - x:
				t.append(b[j][i] - a[j][i] - tmp)
				tmp += t[-1]
			else:
				a[j][i] += tmp
		t.clear()
		tmp = 0
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			if a[i][j] != b[i][j]:
				f = 0
				break
		if not f:
			break
	if f:
		print('Yes')
	else:
		print('No')


import math
T = int(input())
for t in range(T):
	(R, C, X) = list(map(int, input().split()))
	A = []
	B = []
	sum = 0
	for i in range(R):
		A.append(list(map(int, input().split())))
	for i in range(R):
		B.append(list(map(int, input().split())))
	sum = 0
	diff = []
	for y in range(len(A)):
		diff.append([0] * len(A[0]))
		for x in range(len(A[0])):
			v = A[y][x] - B[y][x]
			diff[y][x] = v
			sum = sum + v
	yes = True
	if sum % X != 0:
		yes = False
	else:
		c_sum = sum // X
		diagSum = []
		for i in range(R + C - 1):
			start = i
			y = 0
			temp = 0
			while y < R and start >= 0:
				if start < C:
					temp = temp + diff[y][start]
				y = y + 1
				start = start - 1
			diagSum.append(temp)
		for start in range(X):
			temp = 0
			for i in range(start, len(diagSum), X):
				temp = temp + diagSum[i]
			if temp != c_sum:
				yes = False
				break
		if yes:
			diagSum = []
			for i in range(-R + 1, C):
				start = i
				y = 0
				temp = 0
				while y < R and start < C:
					if start >= 0:
						temp = temp + diff[y][start]
					y = y + 1
					start = start + 1
				diagSum.append(temp)
			for start in range(X):
				temp = 0
				for i in range(start, len(diagSum), X):
					temp = temp + diagSum[i]
				if temp != c_sum:
					yes = False
					break
	if yes:
		print('Yes')
	else:
		print('No')


from sys import stdin
input = stdin.readline

def solve():
	for row in range(r):
		common_sum = 0
		for i in range(c - x, -1, -x):
			common_sum += diff[row][i]
		for i in range(c - x + 1, c):
			temp_sum = 0
			for j in range(i, -1, -x):
				temp_sum += diff[row][j]
			diff[row][i] = temp_sum - common_sum
	for column in range(c - x + 1, c):
		common_sum = 0
		for i in range(r - x, -1, -x):
			common_sum += diff[i][column]
		for i in range(r - x + 1, r):
			temp_sum = 0
			for j in range(i, -1, -x):
				temp_sum += diff[j][column]
			diff[i][column] = temp_sum - common_sum
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			if diff[i][j]:
				return 'No'
	return 'Yes'
for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = [[0] * c for i in range(r)]
	b = [[0] * c for i in range(r)]
	diff = [[0] * c for i in range(r)]
	diff_sum = 0
	for i in range(r):
		a[i] = list(map(int, input().split()))
	for i in range(r):
		b[i] = list(map(int, input().split()))
		for j in range(c):
			temp = a[i][j] - b[i][j]
			diff[i][j] = temp
			diff_sum += temp
	del (a, b)
	if diff_sum % x:
		print('No')
		continue
	print(solve())


t = int(input())
for _ in range(t):
	(r, c, x) = map(int, input().split())
	a = []
	for i in range(r):
		a.append(list(map(int, input().split())))
	b = []
	for i in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		cnt = 0
		arr = []
		for j in range(c):
			if j < c - x + 1:
				if j >= x:
					cnt -= arr[j - x]
				temp = b[i][j] - (a[i][j] + cnt)
				arr.append(temp)
				cnt += temp
				a[i][j] += cnt
			else:
				if j >= x:
					cnt -= arr[j - x]
				arr.append(cnt)
				a[i][j] += cnt
	for j in range(c - x + 1, c):
		cnt = 0
		arr = []
		for i in range(r):
			if i < r - x + 1:
				if i >= x:
					cnt -= arr[i - x]
				temp = b[i][j] - (a[i][j] + cnt)
				arr.append(temp)
				cnt += temp
				a[i][j] += cnt
			else:
				if i >= x:
					cnt -= arr[i - x]
				arr.append(cnt)
				a[i][j] += cnt
	flg = False
	for i in range(r):
		for j in range(c):
			if b[i][j] != a[i][j]:
				flg = True
				break
		if flg:
			break
	if flg:
		print('No')
	else:
		print('Yes')


def solve(A, B, r, c, x):
	for i in range(r):
		(qu, sm) = ([], 0)
		for j in range(c - x + 1):
			gap = B[i][j] - A[i][j] - sm
			A[i][j] = B[i][j]
			qu.append(gap)
			sm += gap
			if len(qu) == x:
				sm -= qu.pop(0)
		for j in range(c - x + 1, c - len(qu)):
			A[i][j] += sm
		for j in range(c - len(qu), c):
			A[i][j] += sm
			sm -= qu.pop(0)
	for j in range(c):
		(qu, sm) = ([], 0)
		for i in range(r - x + 1):
			gap = B[i][j] - A[i][j] - sm
			A[i][j] = B[i][j]
			qu.append(gap)
			sm += gap
			if len(qu) == x:
				sm -= qu.pop(0)
		for i in range(r - x + 1, r - len(qu)):
			A[i][j] += sm
		for i in range(r - len(qu), r):
			A[i][j] += sm
			sm -= qu.pop(0)
	for i in range(r):
		for j in range(c):
			if A[i][j] != B[i][j]:
				return 'No'
	return 'Yes'
for case in range(int(input())):
	(r, c, x) = map(int, input().split())
	(A, B) = ([], [])
	for i in range(r):
		li = list(map(int, input().split()))
		A.append(li)
	for i in range(r):
		li = list(map(int, input().split()))
		B.append(li)
	ans = solve(A, B, r, c, x)
	print(ans)


import numpy as np
for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = []
	b = []
	for _ in range(r):
		l = list(map(int, input().split()))
		a.append(l)
	for _ in range(r):
		l = list(map(int, input().split()))
		b.append(l)
	dic = {}
	for i in range(x):
		for j in range(x):
			dic[i, j] = []
	for i in range(0, r):
		for j in range(0, c):
			c1 = a[i][j] - b[i][j]
			dic[i % x, j % x].append(c1)
	s = []
	s1 = []
	s2 = []
	for i in range(x):
		s = []
		for j in range(x):
			s.append(sum(dic[i, j]))
		s1.append(s)
	for i in range(x):
		mini = min(s1[i])
		for j in range(x):
			s1[i][j] -= mini
	for j in range(x):
		s = []
		for i in range(x):
			s.append(s1[i][j])
		s2.append(s)
	for i in range(x):
		mini = min(s2[i])
		for j in range(x):
			s2[i][j] -= mini
	flag = 0
	for i in range(x):
		for j in range(x):
			if s2[i][j] != 0:
				flag = 1
				break
	if flag == 0:
		print('Yes')
	else:
		print('No')


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda : list(map(int, sys.stdin.readline().rstrip('\r\n').split()))
mod = 10 ** 9 + 7
Mod = 998244353
INF = float('inf')
tc = 1
tc = int(input())
for _ in range(tc):
	(r, c, x) = inp()
	a = [inp() for i in range(r)]
	b = [inp() for i in range(r)]
	col = [[0] * c for i in range(r + 1)]
	f = True
	for i in range(r):
		row = [0] * (c + 1)
		for j in range(c):
			diff = b[i][j] - (a[i][j] + row[j] + col[i][j])
			if j + x - 1 < c:
				row[j] += diff
				row[j + x] -= diff
			elif i + x - 1 < r:
				col[i][j] += diff
				col[i + x][j] -= diff
			elif diff != 0:
				f = False
			row[j + 1] += row[j]
			col[i + 1][j] += col[i][j]
	print('Yes' if f else 'No')


for _ in range(int(input())):
	(r, c, x) = map(int, input().split())
	a = []
	for i in range(r):
		t = list(map(int, input().split()))
		a.append(t)
	b = []
	for i in range(r):
		t = list(map(int, input().split()))
		b.append(t)
	if x == 2:
		pathsum = 0
		for i in range(r):
			for j in range(c):
				if (r - i + c - j) % 2 == 0:
					mul = -1
				else:
					mul = 1
				if not (i == r - 1 and j == c - 1):
					pathsum += mul * (a[i][j] - b[i][j])
		if a[r - 1][c - 1] - pathsum == b[r - 1][c - 1]:
			print('Yes')
		else:
			print('No')
	else:
		suma = 0
		for i in range(r):
			for j in range(c):
				suma += a[i][j]
		sumb = 0
		for i in range(r):
			for j in range(c):
				sumb += b[i][j]
		diff = [[0 for i in range(c)] for j in range(r)]
		for i in range(r):
			for j in range(c):
				diff[i][j] = b[i][j] - a[i][j]
		for j in range(c):
			curr = 0
			currid = r - x
			while currid >= 0:
				curr += diff[currid][j]
				currid -= x
			for i in range(r - 1, r - x, -1):
				temp = i - x
				while temp >= 0:
					diff[i][j] += diff[temp][j]
					temp -= x
				diff[i][j] -= curr
		for i in range(r - 1, r - x, -1):
			curr = 0
			currid = c - x
			while currid >= 0:
				curr += diff[i][currid]
				currid -= x
			for j in range(c - 1, c - x, -1):
				temp = j - x
				while temp >= 0:
					diff[i][j] += diff[i][temp]
					temp -= x
				diff[i][j] -= curr
		flg = True
		for i in range(r - 1, r - x, -1):
			for j in range(c - 1, c - x, -1):
				if diff[i][j] != 0:
					flg = False
		if flg:
			print('Yes')
		else:
			print('No')


from collections import deque
t = int(input())
while t:
	(r, c, x) = map(int, input().split())
	(a, b) = ([], [])
	for i in range(r):
		a.append(list(map(int, input().split())))
	for i in range(r):
		b.append(list(map(int, input().split())))
	for i in range(r):
		d = deque()
		s = 0
		for j in range(c):
			if j >= x:
				p = d[0]
				d.popleft()
				s -= p
			if j >= c - x + 1:
				a[i][j] += s
			else:
				ads = b[i][j] - a[i][j] - s
				d.append(ads)
				a[i][j] = b[i][j]
				s += ads
	for j in range(c - x + 1, c):
		d = deque()
		s = 0
		for i in range(r):
			if i >= x:
				p = d[0]
				d.popleft()
				s -= p
			if i >= r - x + 1:
				a[i][j] += s
			else:
				ads = b[i][j] - a[i][j] - s
				d.append(ads)
				a[i][j] = b[i][j]
				s += ads
	f = 0
	for i in range(r - x + 1, r):
		for j in range(c - x + 1, c):
			if a[i][j] != b[i][j]:
				f = 1
				break
		if f == 1:
			break
	if f == 0:
		print('Yes')
	else:
		print('No')
	t -= 1


def answer():
	for i in range(n):
		for j in range(m):
			a[i][j] = b[i][j] - a[i][j]
	for i in range(n):
		(d, ind, c, dec) = ([0], 1, 0, 0)
		for j in range(m - x + 1):
			d.append(a[i][j] - dec)
			dec += d[-1]
			c += 1
			a[i][j] = 0
			if c >= x:
				dec -= d[ind]
				ind += 1
		for j in range(m - x + 1, m):
			a[i][j] -= dec
			c += 1
			if c >= x:
				dec -= d[ind]
				ind += 1
	for j in range(m):
		(d, ind, c, dec) = ([0], 1, 0, 0)
		for i in range(n - x + 1):
			d.append(a[i][j] - dec)
			dec += d[-1]
			c += 1
			a[i][j] = 0
			if c >= x:
				dec -= d[ind]
				ind += 1
		for i in range(n - x + 1, n):
			a[i][j] -= dec
			c += 1
			if c >= x:
				dec -= d[ind]
				ind += 1
	ans = a.count([0 for i in range(m)])
	if ans == n:
		return 'Yes'
	return 'No'
for T in range(int(input())):
	(n, m, x) = map(int, input().split())
	a = [list(map(int, input().split())) for i in range(n)]
	b = [list(map(int, input().split())) for i in range(n)]
	print(answer())


import numpy as np
for _ in range(int(input())):
	(row, col, x) = map(int, input().split())
	a = np.array([input().split() for _ in range(row)], int)
	b = np.array([input().split() for _ in range(row)], int)
	diff = a - b
	for r in range(row):
		for c in range(col - x + 1):
			diff[r, c:c + x] -= diff[r, c]
	for r in range(row - x + 1):
		for c in range(col - x + 1, col):
			diff[r:r + x, c] -= diff[r, c]
	print('Yes' if np.all(diff == 0) else 'No')


ram = int(input())
for lokesh in range(ram):
	(m, n, x) = map(int, input().split())
	if x > 10:
		a = []
		b = []
		for i in range(m):
			q = list(map(int, input().split()))
			a.append(q)
		for i in range(m):
			q = list(map(int, input().split()))
			b.append(q)
		for i in range(m):
			l = [0] * x
			s = 0
			for j in range(n - x + 1):
				a[i][j] = a[i][j] + s
				l.append(b[i][j] - a[i][j])
				w = l.pop(0)
				s = s + b[i][j] - a[i][j] - w
				a[i][j] = b[i][j]
			for j in range(n - x + 1, n):
				l.append(0)
				w = l.pop(0)
				s = s - w
				a[i][j] = a[i][j] + s
		for j in range(n - x + 1, n):
			l = [0] * x
			s = 0
			for i in range(m - x + 1):
				a[i][j] = a[i][j] + s
				l.append(b[i][j] - a[i][j])
				w = l.pop(0)
				s = s + b[i][j] - a[i][j] - w
				a[i][j] = b[i][j]
			for i in range(m - x + 1, m):
				l.append(0)
				w = l.pop(0)
				s = s - w
				a[i][j] = a[i][j] + s
		wen = 0
		for i in range(m - x, m):
			for j in range(n - x, n):
				if a[i][j] != b[i][j]:
					wen = 1
					break
			if wen == 1:
				break
		if wen == 1:
			print('No')
		else:
			print('Yes')
	else:
		a = []
		b = []
		for i in range(m):
			q = list(map(int, input().split()))
			a.append(q)
		for i in range(m):
			q = list(map(int, input().split()))
			b.append(q)
		for i in range(m):
			for j in range(n - x + 1):
				r = a[i][j]
				for k in range(j, j + x):
					a[i][k] = a[i][k] - (r - b[i][j])
		for j in range(n - x + 1, n):
			for i in range(m - x + 1):
				s = a[i][j]
				for k in range(i, i + x):
					a[k][j] = a[k][j] - (s - b[i][j])
		wen = 0
		for i in range(m - x, m):
			for j in range(n - x, n):
				if a[i][j] != b[i][j]:
					wen = 1
					break
			if wen == 1:
				break
		if wen == 1:
			print('No')
		else:
			print('Yes')


import sys

def shrink_matrix(matrix, is_row_large=True):
	if len(matrix) > x:
		if r <= 2 * x - 1:
			for i in range(1, r - x):
				for j in range(len(matrix[0])):
					matrix[i][j] = matrix[i][j] + matrix[i + x][j] - matrix[x][j]
			for i in range(r - x, x):
				for j in range(len(matrix[0])):
					matrix[i][j] -= matrix[x][j]
			while len(matrix) > x:
				matrix.pop()
		else:
			while len(matrix) > 2 * x - 1:
				for i in range(x - 1):
					for j in range(len(matrix[0])):
						matrix[-1 - x - i][j] += matrix[-1 - i][j] - matrix[-x][j]
				for i in range(x):
					matrix.pop()
			if len(matrix) > x and len(matrix) <= 2 * x - 1:
				for i in range(1, len(matrix) - x):
					for j in range(len(matrix[0])):
						matrix[i][j] = matrix[i][j] + matrix[i + x][j] - matrix[x][j]
				for i in range(len(matrix) - x, x):
					for j in range(len(matrix[0])):
						matrix[i][j] -= matrix[x][j]
				while len(matrix) > x:
					matrix.pop()
	if len(matrix[0]) > x:
		if c <= 2 * x - 1:
			for i in range(1, c - x):
				for j in range(len(matrix)):
					matrix[j][i] += matrix[j][i + x] - matrix[j][x]
			for i in range(c - x, x):
				for j in range(len(matrix)):
					matrix[j][i] -= matrix[j][x]
			while len(matrix[0]) > x:
				for i in matrix:
					i.pop()
		else:
			while len(matrix[0]) > 2 * x - 1:
				for i in range(x - 1):
					for j in range(len(matrix)):
						matrix[j][-1 - x - i] += matrix[j][-1 - i] - matrix[j][-x]
				for i in range(x):
					for j in matrix:
						j.pop()
			if len(matrix[0]) > x:
				for i in range(1, len(matrix[0]) - x):
					for j in range(len(matrix)):
						matrix[j][i] += matrix[j][i + x] - matrix[j][x]
				for i in range(len(matrix[0]) - x, x):
					for j in range(len(matrix)):
						matrix[j][i] -= matrix[j][x]
				while len(matrix[0]) > x:
					for i in matrix:
						i.pop()
	if len(matrix[0]) == x:
		for i in range(len(matrix)):
			target = matrix[i][-1]
			for j in range(x):
				matrix[i][-1 - j] -= target
			matrix[i].pop()

def is_possible(matrix):
	for i in range(x - 1):
		target = matrix[0][i]
		for j in range(1, x):
			if matrix[j][i] != target:
				return False
	return True
t = int(sys.stdin.readline().rstrip())
for _ in range(t):
	(r, c, x) = list(map(int, sys.stdin.readline().rstrip().split()))
	matrix1 = []
	matrix2 = []
	for _ in range(r):
		matrix1.append(list(map(int, sys.stdin.readline().rstrip().split())))
	for _ in range(r):
		matrix2.append(list(map(int, sys.stdin.readline().rstrip().split())))
	differ_matrix = [[matrix1[i][j] - matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]
	shrink_matrix(differ_matrix)
	if is_possible(differ_matrix):
		print('Yes')
	else:
		print('No')


t = int(input())
while t > 0:
	(r, c, x) = [int(i) for i in input().split()]
	a = []
	for i in range(r):
		a.append([int(i) for i in input().split()])
	b = []
	for i in range(r):
		b.append([int(i) for i in input().split()])
	if x == 2:
		ok = True
		for i in range(r):
			for j in range(c):
				if a[i][j] == b[i][j]:
					continue
				else:
					y = b[i][j] - a[i][j]
					if i < r - 1:
						a[i][j] = b[i][j]
						a[i + 1][j] += y
					elif j < c - 1:
						a[i][j] = b[i][j]
						a[i][j + 1] += y
					else:
						ok = False
		if ok:
			print('Yes')
		else:
			print('No')
	elif x == 3:
		ok = True
		for i in range(r):
			for j in range(c):
				if a[i][j] == b[i][j]:
					continue
				else:
					y = b[i][j] - a[i][j]
					if i < r - 2:
						a[i][j] = b[i][j]
						a[i + 2][j] += y
						a[i + 1][j] += y
					elif j < c - 2:
						a[i][j] = b[i][j]
						a[i][j + 2] += y
						a[i][j + 1] += y
					else:
						ok = False
		if ok:
			print('Yes')
		else:
			print('No')
	else:
		ok = True
		p = [[0 for i in range(c)] for i in range(r + 1)]
		for i in range(r):
			cc = [0 for i in range(c + 1)]
			let = 0
			if i > 0:
				for k in range(c):
					p[i][k] += p[i - 1][k]
			for j in range(c):
				let += cc[j]
				if a[i][j] + let + p[i][j] == b[i][j]:
					a[i][j] = b[i][j]
				else:
					y = b[i][j] - (a[i][j] + let + p[i][j])
					if i < r - x + 1:
						a[i][j] = b[i][j]
						p[i + 1][j] += y
						p[i + x][j] -= y
					elif j < c - x + 1:
						a[i][j] = b[i][j]
						cc[j + 1] += y
						cc[j + x] -= y
					else:
						ok = False
		if ok:
			print('Yes')
		else:
			print('No')
	t -= 1


import numpy as np
t = int(input())
for _ in range(t):
	(r, c, x) = map(int, input().split())
	arr1 = []
	arr2 = []
	for _ in range(r):
		arr1.append(list(map(int, input().split())))
	for _ in range(r):
		arr2.append(list(map(int, input().split())))
	if x == 2 or x == 3:
		for i in range(r):
			for j in range(c):
				arr2[i][j] -= arr1[i][j]
		for i in range(r):
			for j in range(c - x + 1):
				tmp = arr2[i][j]
				for p in range(x):
					arr2[i][j + p] -= tmp
		for j in range(c - x + 1, c):
			for i in range(r - x + 1):
				tmp = arr2[i][j]
				for p in range(x):
					arr2[i + p][j] -= tmp
		possible = True
		refList = [0] * (x - 1)
		for i in range(r - x + 1, r):
			if arr2[i][c - x + 1:c] != refList:
				possible = False
				break
		if possible:
			print('Yes')
		else:
			print('No')
	else:
		arr1 = np.matrix(arr1)
		arr2 = np.matrix(arr2)
		arr2 -= arr1
		for i in range(r):
			for j in range(c - x + 1):
				arr2[i, j:j + x] -= arr2[i, j]
		for j in range(c - x + 1, c):
			for i in range(r - x + 1):
				arr2[i:i + x, j] -= arr2[i, j]
		if arr2[r - x + 1:r, c - x + 1:c].any() != 0:
			print('No')
		else:
			print('Yes')


def invariants(M, X):
	R = len(M)
	C = len(M[0])
	ret = [[0 for _ in range(X - 1)] for _ in range(X - 1)]
	for i in range(R):
		r1 = i % X
		for j in range(C):
			r2 = j % X
			if r1 != X - 1 and r2 != X - 1:
				ret[r1][r2] += M[i][j]
			if r1 != X - 1 and r2 != 0:
				ret[r1][r2 - 1] -= M[i][j]
			if r1 != 0 and r2 != X - 1:
				ret[r1 - 1][r2] -= M[i][j]
			if r1 != 0 and r2 != 0:
				ret[r1 - 1][r2 - 1] += M[i][j]
	return ret
for _ in range(int(input())):
	(R, C, X) = map(int, input().split())
	(A, B) = ([], [])
	for _ in range(R):
		A.append(list(map(int, input().split())))
	for _ in range(R):
		B.append(list(map(int, input().split())))
	M = [[A[i][j] - B[i][j] for j in range(C)] for i in range(R)]
	invs = sum(invariants(M, X), [])
	print('No' if any(invs) else 'Yes')