Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0], -grid[0][0]}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    s = dp[h][w]
    limit = (H - h + 1) * (W - w + 1) * 80
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            if abs(c + g) <= limit:
                s.add(abs(c + g))
            if abs(c - g) <= limit:
                s.add(abs(c - g))
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            if abs(c + g) <= limit:
                s.add(abs(c + g))
            if abs(c - g) <= limit:
                s.add(abs(c - g))

ans = INF
for s in dp[-1][-1]:
    ans = min(ans, abs(s))

print(ans)


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0] % 800, -(grid[0][0]) % 800}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    s = dp[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
# print(grid)
# print(dp)
ans = INF
for s in dp[-1][-1]:
    ans = min(ans, s)
    ans = min(ans, (-s) % 800)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

LIMIT = 80 * 80
# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0], -grid[0][0]}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    s = dp[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            if abs(c + g) <= LIMIT:
                s.add(abs(c + g))
            if abs(c - g) <= LIMIT:
                s.add(abs(c - g))
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            if abs(c + g) <= LIMIT:
                s.add(abs(c + g))
            if abs(c - g) <= LIMIT:
                s.add(abs(c - g))

ans = INF
for s in dp[-1][-1]:
    ans = min(ans, abs(s))

print(ans)


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0] % 800, -(grid[0][0]) % 800}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    s = dp[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
# print(grid)
# print(dp)
ans = INF
for s in dp[-1][-1]:
    ans = min(ans, s)
    ans = min(ans, (-s) % 800)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

LIMIT = 80 * 40 * 40
# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0], -grid[0][0]}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            if -LIMIT <= c + g <= LIMIT:
                dp[h][w].add(c + g)
            if -LIMIT <= c - g <= LIMIT:
                dp[h][w].add(c - g)
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            if -LIMIT <= c + g <= LIMIT:
                dp[h][w].add(c + g)
            if -LIMIT <= c - g <= LIMIT:
                dp[h][w].add(c - g)

ans = INF
for s in dp[-1][-1]:
    ans = min(ans, abs(s))

print(ans)


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0] % 800, -(grid[0][0]) % 800}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    s = dp[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
# print(grid)
# print(dp)
ans = INF
for s in dp[-1][-1]:
    ans = min(ans, s)
    ans = min(ans, (-s) % 800)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0], -grid[0][0]}
for h, w in itertools.product(range(H), range(W)):
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            dp[h][w].add(c + grid[h][w])
            dp[h][w].add(c - grid[h][w])
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            dp[h][w].add(c + grid[h][w])
            dp[h][w].add(c - grid[h][w])

ans = INF
for s in dp[-1][-1]:
    ans = min(ans, abs(s))

print(ans)


Fixed submission:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(range(H), range(W)):
    grid[h][w] = A[h][w] - B[h][w]

# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0] % 800, -(grid[0][0]) % 800}
for h, w in itertools.product(range(H), range(W)):
    g = grid[h][w]
    s = dp[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
# print(grid)
# print(dp)
ans = INF
for s in dp[-1][-1]:
    ans = min(ans, s)
    ans = min(ans, (-s) % 800)
print(ans)