Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)]


def main():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    q = deque()
    q.append((0, 0, 1))
    sp = -1
    while len(q):
        i, j, path = q.popleft()
        if i == H-1 and j == W-1:
            sp = path
            break
        if S[i][j] != '.' and S[i][j] < path:
            continue
        S[i][j] = path
        for d in dirs:
            ni, nj = i+d[0], j+d[1]
            if ni >= 0 and nj >= 0 and ni < H and nj < W and S[ni][nj] != '#':
                if S[ni][nj] == '.' or path+1 < S[ni][nj]:
                    q.append((ni, nj, path+1))
    if sp == -1:
        print(sp)
    else:
        ans = 0
        for i in range(H):
            for j in range(W):
                if S[i][j] != '#':
                    ans += 1
        print(ans - sp)


if __name__ == "__main__":
    main()


Fixed submission:
directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]


def main():
    H, W = map(int, input().split())
    s = [input() for _ in range(H)]
    path = [[float("inf")] * W for _ in range(H)]
    white = sum([len(row.replace("#", "")) for row in s])
    scan = [(0, 0, 1)]  # i, j, cost
    while len(scan):
        i, j, cost = scan.pop(0)
        if cost >= path[i][j]:
            continue
        path[i][j] = cost
        for d in directions:
            nexti = i + d[0]
            nextj = j + d[1]
            if 0 <= nexti and nexti < H and \
               0 <= nextj and nextj < W and \
               s[nexti][nextj] == ".":
                scan.append((nexti, nextj, cost + 1))
    if path[H-1][W-1] == float("inf"):
        print(-1)
    else:
        print(white - path[H-1][W-1])


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)]


def main():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    q = deque()
    q.append((0, 0, 1))
    sp = -1
    while len(q):
        i, j, path = q.popleft()
        if i == H-1 and j == W-1:
            sp = path
            break
        if S[i][j] != '.' and S[i][j] < path:
            continue
        S[i][j] = path
        for d in dirs:
            ni, nj = i+d[0], j+d[1]
            if ni >= 0 and nj >= 0 and ni < H and nj < W and S[ni][nj] != '#':
                q.append((ni, nj, path+1))
    if sp == -1:
        print(sp)
    else:
        ans = 0
        for i in range(H):
            for j in range(W):
                if S[i][j] != '#':
                    ans += 1
        print(ans - sp)


if __name__ == "__main__":
    main()


Fixed submission:
directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]


def main():
    H, W = map(int, input().split())
    s = [input() for _ in range(H)]
    path = [[float("inf")] * W for _ in range(H)]
    white = sum([len(row.replace("#", "")) for row in s])
    scan = [(0, 0, 1)]  # i, j, cost
    while len(scan):
        i, j, cost = scan.pop(0)
        if cost >= path[i][j]:
            continue
        path[i][j] = cost
        for d in directions:
            nexti = i + d[0]
            nextj = j + d[1]
            if 0 <= nexti and nexti < H and \
               0 <= nextj and nextj < W and \
               s[nexti][nextj] == ".":
                scan.append((nexti, nextj, cost + 1))
    if path[H-1][W-1] == float("inf"):
        print(-1)
    else:
        print(white - path[H-1][W-1])


if __name__ == "__main__":
    main()