Problem p02735 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
def main():
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]
    dp = [[10**5] * w for _ in range(h)]
    def func(temp, now, flag):
        x, y = now
        if y > h - 1 or x > w - 1:
            return 10**6
        if s[y][x] == "#":
            if not flag:
                temp += 1
            flag = True
        else:
            flag = False
        if x == w-1 and y == h-1:
            return temp
        if dp[y][x] <= temp:
            return 10**6
        dp[y][x] = temp
        piyo = []
        return min(func(temp, (x + 1, y), flag), func(temp, (x, y + 1), flag))
    print(func(0, (0, 0), False))
main()

Buggy submission (verdict: Runtime Error):
black = 1
count = 0
flag = False
def check(board, now):
    global flag, count
    if now[0] > len(board) -2 or board[now[0]+1][now[1]]==black:
        if now[1] > len(board[0]) -2 or board[now[0]][now[1]+1]==black:
            if flag == False:
                count += 1
                flag = True
            if now[1] < len(board[0]) -1:
                return [now[0], now[1]+1]
            elif now[0] < len(board) -1:
                return [now[0]+1, now[1]]
        else:
            flag = False
            return [now[0], now[1]+1]
    else:
        flag = False
        return [now[0]+1, now[1]]
def main():
    global count, flag
    H, W = map(int, input().split())
    board = [[white for i in range(W)] for j in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                board[i][j] = white
            else:
                board[i][j] = black
    if board[0][0] == black:
        count += 1
        flag = True
    now = [0, 0]
    while now != [H-1, W-1]:
        now = check(board, now)
    print(count)
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def calc2(array):
    temp = []
    for x, y in zip(array[:-1], array[1:]):
        temp.append(abs(x-y))
    if len(temp) == 1:
        print(temp[0])
    else:
        calc2(temp)
def main():
    N = int(input())
    array = [0 for i in range(N+1)]
    s = input()
    for j in range(N):
        array[j+1] = int(s[j])
    calc2(array)
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
white = 0
black = 1
temp = []
def resolve(board, H, W):
    now = [0, 0]
    while now != [H-1, W-1]:
        now = check(board, now)
    return len(temp)
def check(board, now):
    if now[0] > len(board) -1 or board[now[0]+1][now[1]]==black:
        if now[1] > len(board[0]) -1 or board[now[0]][now[1]+1]==black:
            temp.append([now[0], now[1]+1])
            return [now[0], now[1]+1]
        else:
            return [now[0], now[1]+1]
    else:
        return [now[0]+1, now[1]]
def change(board, r0, c0, r1, c1):
    for r in range(r0, r1 + 1):
        for c in range(c0, c1 + 1):
            board[r][c] = 1 - board[r][c]
def main():
    H, W = map(int, input().split())
    board = [[white for i in range(W)] for j in range(H)]
    for i in range(H):
        s = input()
        for j in range(W):
            if s[j] == ".":
                board[i][j] = white
            else:
                board[i][j] = black
    n = resolve(board, H, W)
    print(n)
if __name__ == "__main__":
    main()