Problem p03436 (python)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
inf = float("inf")
s = [["#"] * (W + 2)]
D = [[inf] * (W + 2)]
white_num = 0
for i in range(H):
    main = list(input())
    white_num += len([e for e in main if e == "."])
    s.append(["#"] + main + ["#"])
    D.append([inf] * (W + 2))
s.append(["#"] * (W + 2))
D.append([inf] * (W + 2))


def BFS():  # 幅優先探索
    global D
    D[1][1] = 1
    q = deque()
    q.append((1, 1))
    while len(q) != 0:
        p = q.popleft()
        for e in [(p[0] + 1, p[1]), (p[0], p[1] + 1), (p[0] - 1, p[1]), (p[0], p[1] - 1)]:
            if s[e[0]][e[1]] == "." and D[e[0]][e[1]] == inf:
                D[e[0]][e[1]] = D[p[0]][p[1]] + 1
                q.append(e)
                if e == (H, W):
                    return white_num - D[H][W]
    return -1


print(BFS())

# for i in range(1, H + 1):
#    print(" ".join(list(map(str, D[i][1:-1]))))

Buggy submission (verdict: Runtime Error):
from collections import deque
H, W = map(int, input().split())
inf = float("inf")
s = [["#"] * (W + 2)]
D = [[inf] * (W + 2)]
white_num = 0
for i in range(H):
    main = list(input())
    white_num += len([e for e in main if e == "."])
    s.append(["#"] + main + ["#"])
    D.append([inf] * (W + 2))
s.append(["#"] * (W + 2))
D.append([inf] * (W + 2))


def BFS():  # 幅優先探索
    global D
    D[1][1] = 1
    q = deque()
    q.append((1, 1))
    while len(q) != 0:
        p = q.popleft()
        visited[p[0]][p[1]] = True
        if p == (H, W):  # ゴールを訪問したら全部終わってなくても打ち切る
            return white_num - D[H][W]
        for e in [(p[0] + 1, p[1]), (p[0], p[1] + 1), (p[0] - 1, p[1]), (p[0], p[1] - 1)]:
            if s[e[0]][e[1]] == "." and D[e[0]][e[1]] == inf:
                D[e[0]][e[1]] = D[p[0]][p[1]] + 1
                q.append(e)
    return -1


print(BFS())

# for i in range(1, H + 1):
#    print(" ".join(list(map(str, D[i][1:-1]))))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
inf = float("inf")
s = [["#"] * (W + 2)]
D = [[None] * (W + 2)]
white_num = 0
for i in range(H):
    main = list(input())
    white_num += len([e for e in main if e == "."])
    s.append(["#"] + main + ["#"])
    D.append([None] * (W + 2))
s.append(["#"] * (W + 2))
D.append([None] * (W + 2))


def search(i, j):  # (i,j)からの最短経路長を求める再帰関数
    #print("({},{})".format(i, j))
    if i == H and j == W:
        return 0
    if s[i][j] == "#":
        D[i][j] = inf
        return inf
    if D[i][j] is not None:
        return D[i][j]
    D[i][j] = inf
    D[i][j] = min(search(i - 1, j), search(i, j - 1),
                  search(i, j + 1), search(i + 1, j)) + 1
    return D[i][j]


white_min = search(1, 1) + 1
if white_min == inf:
    print(-1)
else:
    print(white_num - white_min)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
inf = float("inf")
s = [["#"] * (W + 2)]
D = [[None] * (W + 2)]
white_num = 0
for i in range(H):
    main = list(input())
    white_num += len([e for e in main if e == "."])
    s.append(["#"] + main + ["#"])
    D.append([None] * (W + 2))
s.append(["#"] * (W + 2))
D.append([None] * (W + 2))


def search(i, j):  # (i,j)からの最短経路長を求める再帰関数
    #print("({},{})".format(i, j))
    if i == H and j == W:
        return 0
    if s[i][j] == "#":
        return inf
    if D[i][j] is not None:
        return D[i][j]
    D[i][j] = inf
    D[i][j] = min(search(i - 1, j), search(i, j - 1),
                  search(i, j + 1), search(i + 1, j)) + 1
    return D[i][j]


white_min = search(1, 1) + 1
if white_min == inf:
    print(-1)
else:
    print(white_num - white_min)