Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
I=lambda:int(input())
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]

M=80*(H+W)
dp=[[[False]*(M+1) for _ in [0]*W] for _ in [0]*H]
dp[0][0][0]=True

m=M
for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in range(M+1):
            if dp[h][w][a]:
                #配る
                if h!=H-1:
                    if 0<=abs(a+d)<=M:
                        dp[h+1][w][abs(a+d)]=True
                        if (h+1)*w==(H-1)*(W-1):
                            m=min(abs(a+d),m)
                    if 0<=abs(a-d)<=M:
                        dp[h+1][w][abs(a-d)]=True
                        if (h+1)*w==(H-1)*(W-1):
                            m=min(abs(a-d),m)
                if w!=W-1:
                    if 0<=abs(a+d)<=M:
                        dp[h][w+1][abs(a+d)]=True
                        if h*(w+1)==(H-1)*(W-1):
                            m=min(abs(a+d),m)
                    if 0<=abs(a-d)<=M:
                        dp[h][w+1][abs(a-d)]=True
                        if h*(w+1)==(H-1)*(W-1):
                            m=min(abs(a-d),m)

res=dp[H-1][W-1][:]
for a in range(M+1):
    if dp[H-1][W-1][a]:
        d=A[H-1][W-1]-B[H-1][W-1]
        if 0<=abs(a+d)<=M:
            res[abs(a+d)]=True
            m=min(abs(a+d),m)
        if 0<=abs(a-d)<=M:
            res[abs(a-d)]=True
            m=min(abs(a-d),m)
            
print(m)

Fixed submission:
import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]

G=[[set() for i in range(W)] for j in range(H)]
G[0][0].add(0)

for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in G[h][w]:
            if h!=H-1:
                G[h+1][w].add(abs(a+d))
                G[h+1][w].add(abs(a-d))
            if w!=W-1:
                G[h][w+1].add(abs(a+d))
                G[h][w+1].add(abs(a-d))

res=set()
d=A[H-1][W-1]-B[H-1][W-1]
for a in G[H-1][W-1]:
    res.add(abs(a+d))
    res.add(abs(a-d))

res=sorted(list(res))
print(min(res))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
I=lambda:int(input())
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]

M=80*(H+W)
dp=[[[False]*(2*M+1) for _ in [0]*W] for _ in [0]*H]
dp[0][0][M]=True

m=2*M+1
for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in range(2*M+1):
            if dp[h][w][a]:
                #配る
                if h!=H-1:
                    if 0<=a+d<=2*M:
                        dp[h+1][w][a+d]=True
                        if (h+1)*w==(H-1)*(W-1):
                            m=min(abs(a+d-M),m)
                    if 0<=a-d<=2*M:
                        dp[h+1][w][a-d]=True
                        if (h+1)*w==(H-1)*(W-1):
                            m=min(abs(a-d-M),m)
                if w!=W-1:
                    if 0<=a+d<=2*M:
                        dp[h][w+1][a+d]=True
                        if h*(w+1)==(H-1)*(W-1):
                            m=min(abs(a+d-M),m)
                    if 0<=a-d<=2*M:
                        dp[h][w+1][a-d]=True
                        if h*(w+1)==(H-1)*(W-1):
                            m=min(abs(a-d-M),m)
                            
res=dp[H-1][W-1][:]
for a in range(2*M+1):
    if dp[H-1][W-1][a]:
        d=A[H-1][W-1]-B[H-1][W-1]
        if 0<=a+d<=2*M:
            res[a+d]=True
            m=min(abs(a+d-M),m)
        if 0<=a-d<=2*M:
            res[a-d]=True
            m=min(abs(a+d-M),m)
            
print(m)

Fixed submission:
import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]

G=[[set() for i in range(W)] for j in range(H)]
G[0][0].add(0)

for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in G[h][w]:
            if h!=H-1:
                G[h+1][w].add(abs(a+d))
                G[h+1][w].add(abs(a-d))
            if w!=W-1:
                G[h][w+1].add(abs(a+d))
                G[h][w+1].add(abs(a-d))

res=set()
d=A[H-1][W-1]-B[H-1][W-1]
for a in G[H-1][W-1]:
    res.add(abs(a+d))
    res.add(abs(a-d))

res=sorted(list(res))
print(min(res))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A,B=[],[]
for _ in [0]*H:
    A.append(LI())
    
for _ in [0]*H:
    B.append(LI())
    
G=[[set() for i in range(W)] for j in range(H)]
dif=A[0][0]-B[0][0]
G[0][0].add(0)

M=-1
for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        M=max(M,abs(d))

M*=(H+W)
res=[[[False]*(2*M+1) for _ in [0]*W] for _ in [0]*H]

for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in G[h][w]:
            if h!=H-1:
                G[h+1][w].add(a+d)
                G[h+1][w].add(a-d)
            if w!=W-1:
                G[h][w+1].add(a+d)
                G[h][w+1].add(a-d)
    
res=set()
for a in G[H-1][W-1]:
    res.add(a)
d=A[H-1][W-1]-B[H-1][W-1]
for a in G[H-1][W-1]:
    res.add(a+d)
    res.add(a-d)

res=list(res)
res1=sorted([-r for r in res if r<=0])
res2=sorted([r for r in res if r>=0])
print(min(res1[0],res2[0]))

# for h in range(H):
#     for w in range(W):
#         print(list(G[h][w]))

Fixed submission:
import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]

G=[[set() for i in range(W)] for j in range(H)]
G[0][0].add(0)

for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in G[h][w]:
            if h!=H-1:
                G[h+1][w].add(abs(a+d))
                G[h+1][w].add(abs(a-d))
            if w!=W-1:
                G[h][w+1].add(abs(a+d))
                G[h][w+1].add(abs(a-d))

res=set()
d=A[H-1][W-1]-B[H-1][W-1]
for a in G[H-1][W-1]:
    res.add(abs(a+d))
    res.add(abs(a-d))

res=sorted(list(res))
print(min(res))