Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

def main():
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]

    lst=[[0]*w for i in range(h)]

    lst[0][0]={abs(a[0][0]-b[0][0]):1}
    # print(lst)
    d=deque([(0,0)])
    while d:
        now=d.popleft()
        tmp=lst[now[0]][now[1]].keys()
        if now[0]+1<h:
            ab=abs(a[now[0]+1][now[1]]-b[now[0]+1][now[1]])
            if lst[now[0]+1][now[1]]==0:
                d.append((now[0]+1,now[1]))
                lst[now[0]+1][now[1]]={}

            for i in tmp:
                if not i+ab in lst[now[0]+1][now[1]]:
                    lst[now[0]+1][now[1]][i+ab]=1
                if not abs(i-ab) in lst[now[0]+1][now[1]]:
                    lst[now[0]+1][now[1]][abs(i-ab)]=1

        # print(lst)
        if now[1]+1<w:
            ab=abs(a[now[0]][now[1]+1]-b[now[0]][now[1]+1])
            if lst[now[0]][now[1]+1]==0:
                d.append((now[0],now[1]+1))
                lst[now[0]][now[1]+1]={}

            for i in tmp:
                if not i+ab in lst[now[0]][now[1]+1]:
                    lst[now[0]][now[1]+1][i+ab]=1
                if not abs(i-ab) in lst[now[0]][now[1]+1]:
                    lst[now[0]][now[1]+1][abs(i-ab)]=1
        # print(d)
        # print(lst)
    anslst=list(lst[-1][-1].keys())
    for i in range(10**9):
        if i in anslst:
            print(i)
            break

main()

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

def main():
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]

    lst=[[0]*w for i in range(h)]

    lst[0][0]={abs(a[0][0]-b[0][0]):1}
    # print(lst)
    d=deque([(0,0)])
    while d:
        now=d.popleft()
        tmp=lst[now[0]][now[1]].keys()
        if now[0]+1<h:
            ab=abs(a[now[0]+1][now[1]]-b[now[0]+1][now[1]])
            if lst[now[0]+1][now[1]]==0:
                d.append((now[0]+1,now[1]))
                lst[now[0]+1][now[1]]={}

            for i in tmp:
                if not i+ab in lst[now[0]+1][now[1]]:
                    lst[now[0]+1][now[1]][i+ab]=1
                if not abs(i-ab) in lst[now[0]+1][now[1]]:
                    lst[now[0]+1][now[1]][abs(i-ab)]=1

        # print(lst)
        if now[1]+1<w:
            ab=abs(a[now[0]][now[1]+1]-b[now[0]][now[1]+1])
            if lst[now[0]][now[1]+1]==0:
                d.append((now[0],now[1]+1))
                lst[now[0]][now[1]+1]={}

            for i in tmp:
                if not i+ab in lst[now[0]][now[1]+1]:
                    lst[now[0]][now[1]+1][i+ab]=1
                if not abs(i-ab) in lst[now[0]][now[1]+1]:
                    lst[now[0]][now[1]+1][abs(i-ab)]=1
        # print(d)
        # print(lst)
    anslst=list(lst[-1][-1].keys())
    anslst.sort(key=lambda x: abs(x))
    print(abs(anslst[0]))

main()

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

def main():
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]

    lst=[[0]*w for i in range(h)]

    lst[0][0]={abs(a[0][0]-b[0][0]):1}
    # print(lst)
    d=deque([(0,0)])
    while d:
        now=d.popleft()
        tmp=lst[now[0]][now[1]].keys()
        if now[0]+1<h:
            ab=abs(a[now[0]+1][now[1]]-b[now[0]+1][now[1]])
            if lst[now[0]+1][now[1]]==0:
                d.append((now[0]+1,now[1]))
                lst[now[0]+1][now[1]]={}

            for i in tmp:
                lst[now[0]+1][now[1]][i+ab]=1
                lst[now[0]+1][now[1]][abs(i-ab)]=1

        # print(lst)
        if now[1]+1<w:
            ab=abs(a[now[0]][now[1]+1]-b[now[0]][now[1]+1])
            if lst[now[0]][now[1]+1]==0:
                d.append((now[0],now[1]+1))
                lst[now[0]][now[1]+1]={}

            for i in tmp:
                lst[now[0]][now[1]+1][i+ab]=1
                lst[now[0]][now[1]+1][abs(i-ab)]=1
        # print(d)
        # print(lst)
    anslst=list(lst[-1][-1].keys())
    anslst.sort(key=lambda x: abs(x))
    print(abs(anslst[0]))

main()

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

def main():
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]

    lst=[[0]*w for i in range(h)]

    lst[0][0]={abs(a[0][0]-b[0][0]):1}
    # print(lst)
    d=deque([(0,0)])
    while d:
        now=d.popleft()
        tmp=lst[now[0]][now[1]].keys()
        if now[0]+1<h:
            ab=abs(a[now[0]+1][now[1]]-b[now[0]+1][now[1]])
            if lst[now[0]+1][now[1]]==0:
                d.append((now[0]+1,now[1]))
                lst[now[0]+1][now[1]]={}

            for i in tmp:
                lst[now[0]+1][now[1]][i+ab]=1
                lst[now[0]+1][now[1]][abs(i-ab)]=1

        # print(lst)
        if now[1]+1<w:
            ab=abs(a[now[0]][now[1]+1]-b[now[0]][now[1]+1])
            if lst[now[0]][now[1]+1]==0:
                d.append((now[0],now[1]+1))
                lst[now[0]][now[1]+1]={}

            for i in tmp:
                lst[now[0]][now[1]+1][i+ab]=1
                lst[now[0]][now[1]+1][abs(i-ab)]=1
        # print(d)
        # print(lst)
    anslst=list(lst[-1][-1].keys())
    anslst.sort(key=lambda x: abs(x))
    print(abs(anslst[0]))

main()

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

def main():
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]

    ablst=[[0]*w for i in range(h)]
    ma=0
    for i in range(h):
        for j in range(w):
            ablst[i][j]=abs(a[i][j]-b[i][j])
            ma=max(ma,ablst[i][j])
    lst1=[0]*ma*(h+w+10)

    lst=[[lst1[:] for i in range(w)] for i in range(h)]
    lst[0][0][abs(a[0][0]-b[0][0])]=1


    # print(lst)
    for i in range(h):
        for j in range(w):
            if i+1<h:
                for k in range(len(lst[i][j])):
                    if lst[i][j][k]==1:
                        lst[i+1][j][k+ablst[i+1][j]]=1
                        lst[i+1][j][abs(k-ablst[i+1][j])]=1
            if j+1<w:
                for k in range(len(lst[i][j])):
                    if lst[i][j][k]==1:
                        lst[i][j+1][k+ablst[i][j+1]]=1
                        lst[i][j+1][abs(k-ablst[i][j+1])]=1
        # print(d)
            # print(lst[i][j])
    # anslst=list(lst[-1][-1].keys())
    # anslst.sort(key=lambda x: abs(x))
    # print(lst)
    for i in range(len(lst[-1][-1])):
        if lst[-1][-1][i]==1:
            print(i)
            break

main()

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

ablst=[[0]*w for i in range(h)]
ma=0
for i in range(h):
    for j in range(w):
        ablst[i][j]=abs(a[i][j]-b[i][j])
        ma=max(ma,ablst[i][j])
lst1=[0]*ma*(h+w+10)

lst=[[lst1[:] for i in range(w)] for i in range(h)]
lst[0][0][abs(a[0][0]-b[0][0])]=1


# print(lst)
for i in range(h):
    for j in range(w):
        if i+1<h:
            for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i+1][j][k+ablst[i+1][j]]=1
                    lst[i+1][j][abs(k-ablst[i+1][j])]=1
        if j+1<w:
             for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i][j+1][k+ablst[i][j+1]]=1
                    lst[i][j+1][abs(k-ablst[i][j+1])]=1
    # print(d)
        # print(lst[i][j])
# anslst=list(lst[-1][-1].keys())
# anslst.sort(key=lambda x: abs(x))
# print(lst)
for i in range(len(lst[-1][-1])):
    if lst[-1][-1][i]==1:
        print(i)
        break

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

ablst=[[0]*w for i in range(h)]
ma=0
for i in range(h):
    for j in range(w):
        ablst[i][j]=abs(a[i][j]-b[i][j])
        ma=max(ma,ablst[i][j])
lst1=[0]*ma*(h*w+10)

lst=[[lst1[:] for i in range(w)] for i in range(h)]
lst[0][0][abs(a[0][0]-b[0][0])]=1


# print(lst)
for i in range(h):
    for j in range(w):
        if i+1<h:
            for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i+1][j][k+ablst[i+1][j]]=1
                    lst[i+1][j][abs(k-ablst[i+1][j])]=1
        if j+1<w:
             for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i][j+1][k+ablst[i][j+1]]=1
                    lst[i][j+1][abs(k-ablst[i][j+1])]=1
    # print(d)
        # print(lst[i][j])
# anslst=list(lst[-1][-1].keys())
# anslst.sort(key=lambda x: abs(x))
# print(lst)
for i in range(len(lst[-1][-1])):
    if lst[-1][-1][i]==1:
        print(i)
        break

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

lst=[[0]*w for i in range(h)]

lst[0][0]={abs(a[0][0]-b[0][0]):1}
# print(lst)
for i in range(h):
    for j in range(w):
            tmp=lst[i][j].keys()
            if i+1<h:
                if lst[i+1][j]==0:
                    lst[i+1][j]={}
                ab=abs(a[i+1][j]-b[i+1][j])
                for k in tmp:
                    lst[i+1][j][k+ab]=1
                    lst[i+1][j][abs(k-ab)]=1

            # print(lst)
            if j+1<w:
                if lst[i][j+1]==0:
                    lst[i][j+1]={}
                ab=abs(a[i][j+1]-b[i][j+1])
                for k in tmp:
                    lst[i][j+1][k+ab]=1
                    lst[i][j+1][abs(k-ab)]=1
    # print(d)
    # print(lst)
anslst=list(lst[-1][-1].keys())
anslst.sort(key=lambda x: abs(x))
print(abs(anslst[0]))

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

lst=[[0]*w for i in range(h)]

lst[0][0]={abs(a[0][0]-b[0][0]):1}
# print(lst)
d=deque([(0,0)])
while d:
    now=d.popleft()
    tmp=lst[now[0]][now[1]].keys()
    if now[0]+1<h:
        ab=abs(a[now[0]+1][now[1]]-b[now[0]+1][now[1]])
        if lst[now[0]+1][now[1]]==0:
            d.append((now[0]+1,now[1]))
            lst[now[0]+1][now[1]]={}

        for i in tmp:
            lst[now[0]+1][now[1]][i+ab]=1
            lst[now[0]+1][now[1]][abs(i-ab)]=1

    # print(lst)
    if now[1]+1<w:
        ab=abs(a[now[0]][now[1]+1]-b[now[0]][now[1]+1])
        if lst[now[0]][now[1]+1]==0:
            d.append((now[0],now[1]+1))
            lst[now[0]][now[1]+1]={}

        for i in tmp:
            lst[now[0]][now[1]+1][i+ab]=1
            lst[now[0]][now[1]+1][abs(i-ab)]=1
    # print(d)
    # print(lst)
anslst=list(lst[-1][-1].keys())
anslst.sort(key=lambda x: abs(x))
print(abs(anslst[0]))

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

lst=[[0]*w for i in range(h)]

lst[0][0]={abs(a[0][0]-b[0][0]):1}
# print(lst)
d=deque([(0,0)])
while d:
    now=d.popleft()
    if now[0]+1<h:
        ab=abs(a[now[0]+1][now[1]]-b[now[0]+1][now[1]])
        if lst[now[0]+1][now[1]]==0:
            d.append((now[0]+1,now[1]))
            lst[now[0]+1][now[1]]={}

        for i in lst[now[0]][now[1]].keys():
            lst[now[0]+1][now[1]][i+ab]=1
            lst[now[0]+1][now[1]][abs(i-ab)]=1

    # print(lst)
    if now[1]+1<w:
        ab=abs(a[now[0]][now[1]+1]-b[now[0]][now[1]+1])
        if lst[now[0]][now[1]+1]==0:
            d.append((now[0],now[1]+1))
            lst[now[0]][now[1]+1]={}

        for i in lst[now[0]][now[1]].keys():
            lst[now[0]][now[1]+1][i+ab]=1
            lst[now[0]][now[1]+1][abs(i-ab)]=1
    # print(d)
    # print(lst)
anslst=list(lst[-1][-1].keys())
anslst.sort(key=lambda x: abs(x))
print(abs(anslst[0]))

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

lst=[[0]*w for i in range(h)]

lst[0][0]={abs(a[0][0]-b[0][0]):1}
# print(lst)
d=deque([(0,0)])
while d:
    now=d.popleft()
    if now[0]+1<h:
        if lst[now[0]+1][now[1]]==0:
            d.append((now[0]+1,now[1]))
            lst[now[0]+1][now[1]]={}

        for i in lst[now[0]][now[1]].keys():
            ab=abs(a[now[0]+1][now[1]]-b[now[0]+1][now[1]])
            lst[now[0]+1][now[1]][i+ab]=1
            lst[now[0]+1][now[1]][abs(i-ab)]=1

    # print(lst)
    if now[1]+1<w:
        if lst[now[0]][now[1]+1]==0:
            d.append((now[0],now[1]+1))
            lst[now[0]][now[1]+1]={}

        for i in lst[now[0]][now[1]].keys():
            ab=abs(a[now[0]][now[1]+1]-b[now[0]][now[1]+1])
            lst[now[0]][now[1]+1][i+ab]=1
            lst[now[0]][now[1]+1][abs(i-ab)]=1
    # print(d)
    # print(lst)
anslst=list(lst[-1][-1].keys())
anslst.sort(key=lambda x: abs(x))
print(abs(anslst[0]))

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

lst=[[0]*w for i in range(h)]

lst[0][0]={a[0][0]-b[0][0]:1,-a[0][0]+b[0][0]:1}
# print(lst)
d=deque([(0,0)])
while d:
    now=d.popleft()
    if now[0]+1<h:
        if lst[now[0]+1][now[1]]==0:
            d.append((now[0]+1,now[1]))
            lst[now[0]+1][now[1]]={}

        for i in lst[now[0]][now[1]].keys():
            ab=a[now[0]+1][now[1]]-b[now[0]+1][now[1]]
            lst[now[0]+1][now[1]][i+ab]=1
            lst[now[0]+1][now[1]][i-ab]=1

    # print(lst)
    if now[1]+1<w:
        if lst[now[0]][now[1]+1]==0:
            d.append((now[0],now[1]+1))
            lst[now[0]][now[1]+1]={}

        for i in lst[now[0]][now[1]].keys():
            ab=a[now[0]][now[1]+1]-b[now[0]][now[1]+1]
            lst[now[0]][now[1]+1][i+ab]=1
            lst[now[0]][now[1]+1][i-ab]=1
    # print(d)
    # print(lst)
anslst=list(lst[-1][-1].keys())
anslst.sort(key=lambda x: abs(x))
print(abs(anslst[0]))

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Runtime Error):
import sys
import time
def input():
    return sys.stdin.readline()[:-1]
h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]
kata=[]
for i in range(h):
    tmp=[]
    for j in range(w):
        tmp.append(abs(a[i][j]-b[i][j]))
    kata.append(tmp)
# print(kata)
tmp=0
for i in range(h):
    tmp=max(tmp,max(kata[i]))
# print(tmp)

dp=[[[0 for i in range(tmp*(h+w)+1)] for j in range(w+1)] for k in range(h+1)]
dp[0][0][kata[0][0]]+=1
i=0
j=0
# print(dp)
while i+j!=h+w-2:
    # print(i,j)
    for k in range(tmp*(h+w)+1):
        if dp[i][j][k]!=0:
            if i+1!=h:
                dp[i+1][j][k+kata[i+1][j]]+=1
                dp[i+1][j][abs(k-kata[i+1][j])]+=1
            if j+1!=w:
                dp[i][j+1][k+kata[i][j+1]]+=1
                dp[i][j+1][abs(k-kata[i][j+1])]+=1
    i-=1
    j+=1
    if i<0:
        i=abs(i+j)+1
        j=0
    while i>=h:
        i-=1
        j+=1
    # time.sleep(1)
# print(dp[1][0])
# print(dp[0][1])
# print(dp[h-1][w-1])
for i in range(tmp*(h+w)+1):
    if dp[h-1][w-1][i]!=0:
        print(i)
        quit()

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy python submission (verdict: Runtime Error):
import sys
import time
def input():
    return sys.stdin.readline()[:-1]
h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]
kata=[]
for i in range(h):
    tmp=[]
    for j in range(w):
        tmp.append(abs(a[i][j]-b[i][j]))
    kata.append(tmp)
# print(kata)
tmp=0
for i in range(h):
    tmp=max(tmp,max(kata[i]))
# print(tmp)

dp=[[[0 for i in range(tmp*(h+w)+1)] for j in range(w+1)] for k in range(h+1)]
dp[0][0][kata[0][0]]+=1
i=0
j=0
# print(dp)
while i+j!=h+w-2:
    # print(i,j)
    for k in range(tmp*(h+2)+1):
        if dp[i][j][k]!=0:
            if i+1!=h:
                dp[i+1][j][k+kata[i+1][j]]+=1
                dp[i+1][j][abs(k-kata[i+1][j])]+=1
            if j+1!=w:
                dp[i][j+1][k+kata[i][j+1]]+=1
                dp[i][j+1][abs(k-kata[i][j+1])]+=1
    i-=1
    j+=1
    if i<0:
        i=abs(i+j)+1
        j=0
    while i>=h:
        i-=1
        j+=1
    # time.sleep(1)
# print(dp[1][0])
# print(dp[0][1])
# print(dp[h-1][w-1])
for i in range(tmp*(h+w)+1):
    if dp[h-1][w-1][i]!=0:
        print(i)
        quit()

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()