Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline

h, w = map(int, input().split())
grid = [None for _ in range(h)]
poss = [[set() for x in range(w)] for y in range(h)]
tmp = [map(int, input().split()) for _ in range(h * 2)]
for i, (l1, l2) in enumerate(zip(tmp, tmp[h:])):
	grid[i] = [abs(x - y) for x, y in zip(l1, l2)]
poss[0][0].add(-grid[0][0])
poss[0][0].add(grid[0][0])
for i in range(h):
	for j in range(w - 1):
		for c in poss[i][j]:
			m = grid[i][j + 1]
			poss[i][j + 1].add(c - m)
			poss[i][j + 1].add(c + m)
	if i == h - 1:
		break
	for j in range(w):
		for c in poss[i][j]:
			m = grid[i + 1][j]
			poss[i + 1][j].add(c - m)
			poss[i + 1][j].add(c + m)
print(min(abs(x) for x in poss[-1][-1]))

Fixed submission:
import sys
input = sys.stdin.buffer.readline

h, w = map(int, input().split())
a = [map(int, input().split()) for _ in range(h)]
b = [map(int, input().split()) for _ in range(h)]
g = [list(abs(x - y) for x, y in zip(l1, l2)) for l1, l2 in zip(a, b)]
poss = [[0] * w for _ in range(h)]
poss[0][0] |= 1 << 6400 + g[0][0]
poss[0][0] |= 1 << 6400 - g[0][0]
for i in range(h):
	for j in range(w - 1):
		poss[i][j + 1] |= poss[i][j] << g[i][j + 1]
		poss[i][j + 1] |= poss[i][j] >> g[i][j + 1]
	if i != h - 1:
		for j in range(w):
			poss[i + 1][j] |= poss[i][j] << g[i + 1][j]
			poss[i + 1][j] |= poss[i][j] >> g[i + 1][j]
res = poss[-1][-1]
for i in range(6400):
	if res & (1 << 6400 + i) or res & (1 << 6400 - i):
		print(i)
		exit()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline
from heapq import *

h, w = map(int, input().split())
grid = [None for _ in range(h)]
poss = [[set() for x in range(w)] for y in range(h)]
tmp = [map(int, input().split()) for _ in range(h * 2)]
for i, (l1, l2) in enumerate(zip(tmp, tmp[h:])):
	grid[i] = [abs(x - y) for x, y in zip(l1, l2)]
q = [(0, 0)]
poss[0][0].add(-grid[0][0])
poss[0][0].add(grid[0][0])
while q:
	cx, cy = heappop(q)
	for dx, dy in ((1, 0), (0, 1)):
		nx = cx + dx
		ny = cy + dy
		if not (0 <= nx <= w - 1 and 0 <= ny <= h - 1):
			continue
		m = grid[ny][nx]
		for c in poss[cy][cx]:
			poss[ny][nx].add(c - m)
			poss[ny][nx].add(c + m)
		heappush(q, (nx, ny))
print(min(abs(x) for x in poss[-1][-1]))


Fixed submission:
import sys
input = sys.stdin.buffer.readline

h, w = map(int, input().split())
a = [map(int, input().split()) for _ in range(h)]
b = [map(int, input().split()) for _ in range(h)]
g = [list(abs(x - y) for x, y in zip(l1, l2)) for l1, l2 in zip(a, b)]
poss = [[0] * w for _ in range(h)]
poss[0][0] |= 1 << 6400 + g[0][0]
poss[0][0] |= 1 << 6400 - g[0][0]
for i in range(h):
	for j in range(w - 1):
		poss[i][j + 1] |= poss[i][j] << g[i][j + 1]
		poss[i][j + 1] |= poss[i][j] >> g[i][j + 1]
	if i != h - 1:
		for j in range(w):
			poss[i + 1][j] |= poss[i][j] << g[i + 1][j]
			poss[i + 1][j] |= poss[i][j] >> g[i + 1][j]
res = poss[-1][-1]
for i in range(6400):
	if res & (1 << 6400 + i) or res & (1 << 6400 - i):
		print(i)
		exit()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline
from collections import deque

h, w = map(int, input().split())
grid = [None for _ in range(h)]
poss = [[set() for x in range(w)] for y in range(h)]
tmp = [map(int, input().split()) for _ in range(h * 2)]
for i, (l1, l2) in enumerate(zip(tmp, tmp[h:])):
	grid[i] = [abs(x - y) for x, y in zip(l1, l2)]
q = deque([(0, 0)])
poss[0][0].add(-grid[0][0])
poss[0][0].add(grid[0][0])
while q:
	cx, cy = q.popleft()
	for dx, dy in ((1, 0), (0, 1)):
		nx = cx + dx
		ny = cy + dy
		if not (0 <= nx <= w - 1 and 0 <= ny <= h - 1):
			continue
		m = grid[ny][nx]
		poss[ny][nx] |= set(l + m for l in poss[cy][cx])
		poss[ny][nx] |= set(l - m for l in poss[cy][cx])
		q.append((nx, ny))
print(min(abs(x) for x in poss[-1][-1]))


Fixed submission:
import sys
input = sys.stdin.buffer.readline

h, w = map(int, input().split())
a = [map(int, input().split()) for _ in range(h)]
b = [map(int, input().split()) for _ in range(h)]
g = [list(abs(x - y) for x, y in zip(l1, l2)) for l1, l2 in zip(a, b)]
poss = [[0] * w for _ in range(h)]
poss[0][0] |= 1 << 6400 + g[0][0]
poss[0][0] |= 1 << 6400 - g[0][0]
for i in range(h):
	for j in range(w - 1):
		poss[i][j + 1] |= poss[i][j] << g[i][j + 1]
		poss[i][j + 1] |= poss[i][j] >> g[i][j + 1]
	if i != h - 1:
		for j in range(w):
			poss[i + 1][j] |= poss[i][j] << g[i + 1][j]
			poss[i + 1][j] |= poss[i][j] >> g[i + 1][j]
res = poss[-1][-1]
for i in range(6400):
	if res & (1 << 6400 + i) or res & (1 << 6400 - i):
		print(i)
		exit()