Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

A = [[0]*(W+1)]
for i in range(H):
    A.append([0] + list(map(int, input().split())))

B = [[0]*(W+1)]
for i in range(H):
    B.append([0] + list(map(int, input().split())))

C = [[0 for j in range(W+1)] for i in range(H+1)]
for i in range(H+1):
    for j in range(W+1):
        C[i][j] = A[i][j] - B[i][j]

dp = [[set() for j in range(W+1)] for i in range(H+1)]
dp[0][1] = set([0])
dp[1][0] = set([0])
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in list(dp[i-1][j]):
            if k + C[i][j] <= 6400 and k + C[i][j] >= -6400:
                dp[i][j].add(k + C[i][j])
            if k - C[i][j] <= 6400 and k - C[i][j] >= -6400:
                dp[i][j].add(k - C[i][j])
        for k in list(dp[i][j-1]):
            if k + C[i][j] <= 6400 and k + C[i][j] >= -6400:
                dp[i][j].add(k + C[i][j])
            if k - C[i][j] <= 6400 and k - C[i][j] >= -6400:
                dp[i][j].add(k - C[i][j])

ans = list(dp[H][W])
ans = [abs(i) for i in ans]
print(sorted(ans)[0])

Fixed submission:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

A = [[0]*(W+1)]
for i in range(H):
    A.append([0] + list(map(int, input().split())))

B = [[0]*(W+1)]
for i in range(H):
    B.append([0] + list(map(int, input().split())))

C = [[0 for j in range(W+1)] for i in range(H+1)]
for i in range(H+1):
    for j in range(W+1):
        C[i][j] = abs(A[i][j] - B[i][j])

dp = [[0 for j in range(W+1)] for i in range(H+1)]
dp[0][1] = 1<<6400
dp[1][0] = 1<<6400
for i in range(1,H+1):
    for j in range(1,W+1):
        dp[i][j] = dp[i-1][j]<<C[i][j] | dp[i-1][j]>>C[i][j] | dp[i][j-1]<<C[i][j] | dp[i][j-1]>>C[i][j]

for i in range(6400,INF):
    if dp[H][W]>>i & 1:
        print(i-6400)
        sys.exit()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

A = [[0]*(W+1)]
for i in range(H):
    A.append([0] + list(map(int, input().split())))

B = [[0]*(W+1)]
for i in range(H):
    B.append([0] + list(map(int, input().split())))

C = [[0 for j in range(W+1)] for i in range(H+1)]
for i in range(H+1):
    for j in range(W+1):
        C[i][j] = A[i][j] - B[i][j]

dp = [[set() for j in range(W+1)] for i in range(H+1)]
dp[0][1] = set([0])
dp[1][0] = set([0])
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in list(dp[i-1][j]):
            dp[i][j].add(k + C[i][j])
            dp[i][j].add(k - C[i][j])
        for k in list(dp[i][j-1]):
            dp[i][j].add(k + C[i][j])
            dp[i][j].add(k - C[i][j])

ans = list(dp[H][W])
ans = [abs(i) for i in ans]
print(sorted(ans)[0])

Fixed submission:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

A = [[0]*(W+1)]
for i in range(H):
    A.append([0] + list(map(int, input().split())))

B = [[0]*(W+1)]
for i in range(H):
    B.append([0] + list(map(int, input().split())))

C = [[0 for j in range(W+1)] for i in range(H+1)]
for i in range(H+1):
    for j in range(W+1):
        C[i][j] = abs(A[i][j] - B[i][j])

dp = [[0 for j in range(W+1)] for i in range(H+1)]
dp[0][1] = 1<<6400
dp[1][0] = 1<<6400
for i in range(1,H+1):
    for j in range(1,W+1):
        dp[i][j] = dp[i-1][j]<<C[i][j] | dp[i-1][j]>>C[i][j] | dp[i][j-1]<<C[i][j] | dp[i][j-1]>>C[i][j]

for i in range(6400,INF):
    if dp[H][W]>>i & 1:
        print(i-6400)
        sys.exit()