Buggy python submission (verdict: Time Limit Exceeded):

import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop
import itertools
import math
import copy

H, W = map(int, input().split())

A = []
B = []
for _ in range(H):
    a = list(map(int, input().split()))
    A.append(a)

for _ in range(H):
    b = list(map(int, input().split()))
    B.append(b)

C = []
for i in range(H):
    cs = []
    for j in range(W):
        c = abs(A[i][j] - B[i][j])
        cs.append(c)

    C.append(cs)

D = [[[False for _ in range(H*W*80)] for _ in range(W)] for _ in range(H)]

D[0][0][C[0][0]] = True

# print(D[0][0])

for i in range(1, H+W-1):
    #print("i", i)
    for j in range(i+1):
        #print("j", j)
        x = i - j
        y = j
        #print("x", x, "y", y)

        if 0 <= x <= H-1 and 0 <= y <= W - 1:
            if 0 <= x-1 <= H-1:
                for n, bl in enumerate(D[x-1][y]):
                    if not bl:
                        continue
                    # print(n)

                    n2 = n + C[x][y]
                    D[x][y][n2] = True

                    n2 = abs(n - C[x][y])
                    D[x][y][n2] = True

            if 0 <= y-1 <= W-1:
                for n, bl in enumerate(D[x][y-1]):
                    if not bl:
                        continue

                    # print(n)

                    n2 = n + C[x][y]
                    D[x][y][n2] = True

                    n2 = abs(n - C[x][y])
                    D[x][y][n2] = True

            #print("x", x, "y", y, "dxy",  D[x][y])

ans = 0

for i, bl in enumerate(D[H-1][W-1]):
    if bl:
        ans = i
        break


print(ans)


Fixed submission:

import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop
import itertools
import math
import copy

H, W = map(int, input().split())

A = []
B = []
for _ in range(H):
    a = list(map(int, input().split()))
    A.append(a)

for _ in range(H):
    b = list(map(int, input().split()))
    B.append(b)

C = []
for i in range(H):
    cs = []
    for j in range(W):
        c = abs(A[i][j] - B[i][j])
        cs.append(c)

    C.append(cs)


X = (H+W) * 80
L = X + X + 1

dp = [[0] * W for _ in range(H)]
dp[0][0] = np.zeros(L, np.bool)
d = C[0][0]
dp[0][0][X+d] = 1
dp[0][0][X-d] = 1

for r in range(H):
    for c in range(W):
        if r == 0 and c == 0:
            continue

        l = np.zeros(L, np.bool)
        d = C[r][c]

        if r != 0:
            l[d:] |= dp[r-1][c][:L-d]
            l[:L-d] |= dp[r-1][c][d:]

        if c != 0:
            l[d:] |= dp[r][c-1][:L-d]
            l[:L-d] |= dp[r][c-1][d:]

        dp[r][c] = l

dp = dp[-1][-1]
d = np.where(dp)[0] - X
ans = np.abs(d).min()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop
import itertools
import math
import copy


#read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())

A = []
B = []
for _ in range(H):
    a = list(map(int, input().split()))
    A.append(a)

for _ in range(H):
    b = list(map(int, input().split()))
    B.append(b)

C = []
for i in range(H):
    cs = []
    for j in range(W):
        c = abs(A[i][j] - B[i][j])
        cs.append(c)

    C.append(cs)


def calc(arr):
    # print(arr)
    arr.sort(reverse=True)
    a = 0
    b = 0

    for i in range(len(arr)):
        if a > b:
            b += arr[i]
        else:
            a += arr[i]

    return abs(a-b)


def dfs(x, y, arr):
    # print(arr)
    arr.append(C[y][x])

    if x == W-1 and y == H-1:
        return calc(arr)

    xs = [0, 1]
    ys = [1, 0]

    mn = 10**4
    for i in range(2):
        if x+xs[i] <= W-1 and y+ys[i] <= H-1:
            arr2 = copy.copy(arr)
            a = dfs(x+xs[i], y+ys[i], arr2)

            if a < mn:
                mn = a

    return mn


ans = dfs(0, 0, [])
print(ans)


Fixed submission:

import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop
import itertools
import math
import copy

H, W = map(int, input().split())

A = []
B = []
for _ in range(H):
    a = list(map(int, input().split()))
    A.append(a)

for _ in range(H):
    b = list(map(int, input().split()))
    B.append(b)

C = []
for i in range(H):
    cs = []
    for j in range(W):
        c = abs(A[i][j] - B[i][j])
        cs.append(c)

    C.append(cs)


X = (H+W) * 80
L = X + X + 1

dp = [[0] * W for _ in range(H)]
dp[0][0] = np.zeros(L, np.bool)
d = C[0][0]
dp[0][0][X+d] = 1
dp[0][0][X-d] = 1

for r in range(H):
    for c in range(W):
        if r == 0 and c == 0:
            continue

        l = np.zeros(L, np.bool)
        d = C[r][c]

        if r != 0:
            l[d:] |= dp[r-1][c][:L-d]
            l[:L-d] |= dp[r-1][c][d:]

        if c != 0:
            l[d:] |= dp[r][c-1][:L-d]
            l[:L-d] |= dp[r][c-1][d:]

        dp[r][c] = l

dp = dp[-1][-1]
d = np.where(dp)[0] - X
ans = np.abs(d).min()
print(ans)