Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

lst = [[0]*w for _ in range(h)]
m = 0
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])
        m = max(m,abs(a[i][j] - b[i][j]))

dp = [[[0]*((h+w-1)*m) for _ in range(w)] for _ in range(h)]
dp[0][0][lst[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k in range(m*(h+w-1)):
            if dp[i][j][k]:
                if i+1 < h:
                    if k+lst[i+1][j] < m*(h+w-1):
                        dp[i+1][j][k+lst[i+1][j]] = 1
                    if abs(k-lst[i+1][j]) < m*(h+w-1):
                        dp[i+1][j][abs(k-lst[i+1][j])] = 1
                if j+1 < w:
                    if k+lst[i][j+1] < m*(h+w-1):
                        dp[i][j+1][k+lst[i][j+1]] = 1
                    if abs(k-lst[i][j+1]) < m*(h+w-1):
                        dp[i][j+1][abs(k-lst[i][j+1])] = 1

ans = 10**10
for i in range(m*(h+w-1)):
    if dp[-1][-1][i]:
        ans = min(ans,i)
print(ans)


Fixed submission:
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[0 for _ in range(w)] for _ in range(h)]
lst = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])

dp[0][0] |= 1 << (6400+lst[0][0])
dp[0][0] |= 1 << (6400-lst[0][0])
for i in range(h):
    for j in range(w):
        t = dp[i][j]
        if i+1 < h:
            dp[i+1][j] |= t << lst[i+1][j]
            dp[i+1][j] |= t >> lst[i+1][j]
        if j+1 < w:
            dp[i][j+1] |= t << lst[i][j+1]
            dp[i][j+1] |= t >> lst[i][j+1]

x = dp[-1][-1]
for i in range(6401)[::-1]:
    if (x >> i) & 1:
        print(6400 - i)
        break

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

lst = [[0]*w for _ in range(h)]
m = 0
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])
        m = max(m,abs(a[i][j] - b[i][j]))

dp = [[[0]*(h+w)*m for _ in range(w)] for _ in range(h)]
dp[0][0][lst[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k in range(m*(h+w)):
            if dp[i][j][k]:
                if i+1 < h:
                    if k+lst[i+1][j] < m*(h+w):
                        dp[i+1][j][k+lst[i+1][j]] = 1
                    dp[i+1][j][abs(k-lst[i+1][j])] = 1
                if j+1 < w:
                    if k+lst[i][j+1] < m*(h+w):
                        dp[i][j+1][k+lst[i][j+1]] = 1
                    dp[i][j+1][abs(k-lst[i][j+1])] = 1

ans = 10**10
for i in range(m*(h+w)):
    if dp[-1][-1][i]:
        ans = min(ans,i)
print(ans)


Fixed submission:
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[0 for _ in range(w)] for _ in range(h)]
lst = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])

dp[0][0] |= 1 << (6400+lst[0][0])
dp[0][0] |= 1 << (6400-lst[0][0])
for i in range(h):
    for j in range(w):
        t = dp[i][j]
        if i+1 < h:
            dp[i+1][j] |= t << lst[i+1][j]
            dp[i+1][j] |= t >> lst[i+1][j]
        if j+1 < w:
            dp[i][j+1] |= t << lst[i][j+1]
            dp[i][j+1] |= t >> lst[i][j+1]

x = dp[-1][-1]
for i in range(6401)[::-1]:
    if (x >> i) & 1:
        print(6400 - i)
        break

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[set() for _ in range(w)] for _ in range(h)]
lst = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])

dp[0][0].add(lst[0][0])
for i in range(h):
    for j in range(w):
        for k in dp[i][j]:
            if i+1 < h:
                dp[i+1][j].add(k+lst[i+1][j])
                dp[i+1][j].add(abs(k-lst[i+1][j]))
            if j+1 < w:
                dp[i][j+1].add(k+lst[i][j+1])
                dp[i][j+1].add(abs(k-lst[i][j+1]))
ans = inf
for i in dp[-1][-1]:
    ans = min(ans,i)
print(ans)


Fixed submission:
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[0 for _ in range(w)] for _ in range(h)]
lst = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])

dp[0][0] |= 1 << (6400+lst[0][0])
dp[0][0] |= 1 << (6400-lst[0][0])
for i in range(h):
    for j in range(w):
        t = dp[i][j]
        if i+1 < h:
            dp[i+1][j] |= t << lst[i+1][j]
            dp[i+1][j] |= t >> lst[i+1][j]
        if j+1 < w:
            dp[i][j+1] |= t << lst[i][j+1]
            dp[i][j+1] |= t >> lst[i][j+1]

x = dp[-1][-1]
for i in range(6401)[::-1]:
    if (x >> i) & 1:
        print(6400 - i)
        break

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[defaultdict(int) for _ in range(w)] for _ in range(h)]
lst = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])

dp[0][0][lst[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k,_ in dp[i][j].items():
            if i+1 < h:
                dp[i+1][j][k+lst[i+1][j]] = 1
                dp[i+1][j][abs(k-lst[i+1][j])] = 1
            if j+1 < w:
                dp[i][j+1][k+lst[i][j+1]] = 1
                dp[i][j+1][abs(k-lst[i][j+1])] = 1
ans = inf
for i,_ in dp[-1][-1].items():
    ans = min(ans,i)
print(ans)


Fixed submission:
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[0 for _ in range(w)] for _ in range(h)]
lst = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])

dp[0][0] |= 1 << (6400+lst[0][0])
dp[0][0] |= 1 << (6400-lst[0][0])
for i in range(h):
    for j in range(w):
        t = dp[i][j]
        if i+1 < h:
            dp[i+1][j] |= t << lst[i+1][j]
            dp[i+1][j] |= t >> lst[i+1][j]
        if j+1 < w:
            dp[i][j+1] |= t << lst[i][j+1]
            dp[i][j+1] |= t >> lst[i][j+1]

x = dp[-1][-1]
for i in range(6401)[::-1]:
    if (x >> i) & 1:
        print(6400 - i)
        break

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add((a[0][0],b[0][0]))
dp[0][0].add((b[0][0],a[0][0]))
for i in range(h):
    for j in range(w):
        for (k,l) in dp[i][j]:
            if i+1 < h:
                dp[i+1][j].add((k+a[i+1][j],l+b[i+1][j]))
                dp[i+1][j].add((k+b[i+1][j],l+a[i+1][j]))
            if j+1 < w:
                dp[i][j+1].add((k+a[i][j+1],l+b[i][j+1]))
                dp[i][j+1].add((k+b[i][j+1],l+a[i][j+1]))
ans = inf
for (i,j) in dp[-1][-1]:
    ans = min(ans,abs(i-j))
print(ans)


Fixed submission:
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[0 for _ in range(w)] for _ in range(h)]
lst = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])

dp[0][0] |= 1 << (6400+lst[0][0])
dp[0][0] |= 1 << (6400-lst[0][0])
for i in range(h):
    for j in range(w):
        t = dp[i][j]
        if i+1 < h:
            dp[i+1][j] |= t << lst[i+1][j]
            dp[i+1][j] |= t >> lst[i+1][j]
        if j+1 < w:
            dp[i][j+1] |= t << lst[i][j+1]
            dp[i][j+1] |= t >> lst[i][j+1]

x = dp[-1][-1]
for i in range(6401)[::-1]:
    if (x >> i) & 1:
        print(6400 - i)
        break

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[defaultdict(tuple) for _ in range(w)] for _ in range(h)]
dp[0][0][(a[0][0],b[0][0])] = 1
dp[0][0][(b[0][0],a[0][0])] = 1
for i in range(h):
    for j in range(w):
        for (k,l),m in dp[i][j].items():
            if m == 0:
                continue
            if i+1 < h:
                dp[i+1][j][(k+a[i+1][j],l+b[i+1][j])] = 1
                dp[i+1][j][(k+b[i+1][j],l+a[i+1][j])] = 1
            if j+1 < w:
                dp[i][j+1][(k+a[i][j+1],l+b[i][j+1])] = 1
                dp[i][j+1][(k+b[i][j+1],l+a[i][j+1])] = 1
ans = inf
for (i,j),_ in dp[-1][-1].items():
    ans = min(ans,abs(i-j))
print(ans)


Fixed submission:
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

dp = [[0 for _ in range(w)] for _ in range(h)]
lst = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])

dp[0][0] |= 1 << (6400+lst[0][0])
dp[0][0] |= 1 << (6400-lst[0][0])
for i in range(h):
    for j in range(w):
        t = dp[i][j]
        if i+1 < h:
            dp[i+1][j] |= t << lst[i+1][j]
            dp[i+1][j] |= t >> lst[i+1][j]
        if j+1 < w:
            dp[i][j+1] |= t << lst[i][j+1]
            dp[i][j+1] |= t >> lst[i][j+1]

x = dp[-1][-1]
for i in range(6401)[::-1]:
    if (x >> i) & 1:
        print(6400 - i)
        break