Problem p02735 (python)

Fixed submission:
import sys
input = sys.stdin.readline
from collections import deque
def main():
    H, W = map(int, input().split())
    field = [input().rstrip("\r\n") for _ in range(H)]
    dv = 201
    counts = [[dv for _ in range(W)] for _ in range(H)]
    que = deque([(0, 0)])
    if field[0][0] == "#":
        counts[0][0] = 1
    else:
        counts[0][0] = 0
    while len(que) > 0:
        h, w = que.popleft()
        for dh, dw in [(1, 0), (0, 1)]:
            nh, nw = h + dh, w + dw
            if nh >= H or nw >= W:continue
            if counts[nh][nw] == dv:que.append((nh, nw))
            if field[h][w] != field[nh][nw]:
                counts[nh][nw] = min(counts[nh][nw], counts[h][w]+1)
            else:
                counts[nh][nw] = min(counts[nh][nw], counts[h][w])
    if field[-1][-1] == "#":counts[-1][-1] += 1
    print(counts[-1][-1]//2)
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

H, W = map(int, input().split())
field = ['' for _ in range(H)]
seen = [[False for _ in range(W)] for _ in range(H)]
seen_black = [[False for _ in range(W)] for _ in range(H)]

dx = [0, 1]
dy = [1, 0]
stop = (0, 0)
next_start = (0,0)
def dfs(h, w):
    global stop
    seen[h][w] = True
    for dir in range(2):
        nh = h + dx[dir]
        nw = w + dy[dir]
        
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if field[nh][nw] == '#':
            if nh < stop[0] or nw < stop[0]:continue
            if nh+nw > stop[0]+stop[1]:
                stop = (nh, nw)
            elif nh+nw == stop[0]+stop[1]:
                if stop[0] < nw and field[nh][nw:].count(".") > field[nh:][nw].count("."):
                    stop = (nh, nw)
                if stop[1] < nh and field[nh][nw:].count(".") < field[nh:][nw].count("."):
                    stop = (nh, nw)
            continue
        if seen[nh][nw] == True:
            continue
        dfs(nh, nw)
def dfs_black(h, w):
    global next_start
    seen_black[h][w] = True
    for dir in range(2):
        nh = h + dx[dir]
        nw = w + dy[dir]
        
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if field[nh][nw] == '.':
            if nh < next_start[0] or nw < next_start[0]:continue
            if nh+nw > next_start[0]+next_start[1]:
                next_start = (nh, nw)
            continue
        if seen_black[nh][nw] == True:
            continue
        dfs_black(nh, nw)    

for h in range(H):
    field[h] = input().rstrip("\r\n")

sh, sw, gh, gw = 0, 0, H-1, W-1
ans = 0
is_next = True
if field[sh][sw] == "#":
    is_next = False
else:
    is_next = True

while seen[gh][gw] != True and seen_black[gh][gw] != True:
    if is_next:
        dfs(sh,sw)
        sh, sw = stop
        is_next = False
    else:
        ans += 1
        dfs_black(sh,sw)
        sh, sw = next_start
        is_next = True
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
field = ['' for _ in range(H)]
seen = [[False for _ in range(W)] for _ in range(H)]
seen_black = [[False for _ in range(W)] for _ in range(H)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
stop = (0, 0)
next_start = (0,0)
def dfs(h, w):
    global stop
    seen[h][w] = True
    for dir in range(4):
        nh = h + dx[dir]
        nw = w + dy[dir]
        
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if field[nh][nw] == '#':
            step = max(stop[0]+stop[1], nh+nw)
            if step != stop[0]+stop[1]:stop = (nh, nw)
            continue
        if seen[nh][nw] == True:
            continue
        dfs(nh, nw)
def dfs_black(h, w):
    global next_start
    seen_black[h][w] = True
    for dir in range(4):
        nh = h + dx[dir]
        nw = w + dy[dir]
        
        if nh < 0 or nh >= H or nw < 0 or nw >= W:
            continue
        if field[nh][nw] == '.':
            step = max(next_start[0]+next_start[1], nh+nw)
            if step != next_start[0]+next_start[1]:next_start = (nh, nw)
            continue
        if seen_black[nh][nw] == True:
            continue
        dfs_black(nh, nw)    

for h in range(H):
    field[h] = input().rstrip("\r\n")

sh, sw, gh, gw = 0, 0, H-1, W-1
ans = 0
is_next = True
if field[sh][sw] == "#":
    is_next = False
else:
    is_next = True

while seen[gh][gw] != True and seen_black[gh][gw] != True:
    if is_next:
        dfs(sh,sw)
        sh, sw = stop
        is_next = False
    else:
        ans += 1
        # if sh == gh and sw == gw:
        #     break
        dfs_black(sh,sw)
        sh, sw = next_start
        is_next = True
print(ans)