Buggy python submission (verdict: Time Limit Exceeded):
import sys
import functools
import itertools
import math
from collections import deque

def bfs(H, W, S):
    visited = [[False]*(W+2) for _ in range(H+2)]
    unvisited = deque([(1,1)])
    dist = 1
    while unvisited:
        dist += 1
        un = deque()
        for x, y in unvisited:
            visited[y][x] = True
            if S[y-1][x] != '#' and visited[y-1][x] == False:
                un.append((x, y-1))

            if S[y+1][x] != '#' and visited[y+1][x] == False:
                if y+1 == H and x == W:
                    return dist
                un.append((x, y+1))

            if S[y][x-1] != '#' and visited[y][x-1] == False:
                un.append((x-1, y))

            if S[y][x+1] != '#' and visited[y][x+1] == False:
                if y == H and x+1 == W:
                    return dist
                un.append((x+1, y))
        unvisited = un

    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.append('#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

def bfs(H, W, S):
    visited = [[False]*(W+2) for _ in range(H+2)]
    visited[1][1] = True
    unvisited = deque([(1,1)])
    dist = 1
    while unvisited:
        dist += 1
        un = deque()
        for x, y in unvisited:
            for t1, t2 in zip([-1,+1,0,0],[0,0,-1,1]):
                dx = x + t1
                dy = y + t2
                if S[dy][dx] != '#' and not visited[dy][dx]:
                    if dy == H and dx == W:
                        return dist
                    un.append((dx,dy))
                    visited[dy][dx] = True
        unvisited = un

    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.append('#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import functools
import itertools
import math
from collections import deque

def bfs(H, W, S):
    visited = deque()
    unvisited = deque([(1,1)])
    dist = 1
    while unvisited:
        dist += 1
        un = deque()
        for x, y in unvisited:
            visited.append((x,y))
            if S[y-1][x] != '#' and (x, y-1) not in visited:
                un.append((x, y-1))

            if S[y+1][x] != '#' and (x, y+1) not in visited:
                if y+1 == H and x == W:
                    return dist
                un.append((x, y+1))

            if S[y][x-1] != '#' and (x-1, y) not in visited:
                un.append((x-1, y))

            if S[y][x+1] != '#' and (x+1, y) not in visited:
                if y == H and x+1 == W:
                    return dist
                un.append((x+1, y))
        unvisited = un

    return -1

def main():
    H, W = map(int, input().split())
    S = [list('#'+input()+'#') for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.insert(H+1,'#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

def bfs(H, W, S):
    visited = [[False]*(W+2) for _ in range(H+2)]
    visited[1][1] = True
    unvisited = deque([(1,1)])
    dist = 1
    while unvisited:
        dist += 1
        un = deque()
        for x, y in unvisited:
            for t1, t2 in zip([-1,+1,0,0],[0,0,-1,1]):
                dx = x + t1
                dy = y + t2
                if S[dy][dx] != '#' and not visited[dy][dx]:
                    if dy == H and dx == W:
                        return dist
                    un.append((dx,dy))
                    visited[dy][dx] = True
        unvisited = un

    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.append('#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import functools
import itertools
import math
from collections import deque

def bfs(H, W, S):
    visited = deque()
    unvisited = deque([(1,1)])
    dist = 1
    while unvisited:
        dist += 1
        un = deque()
        for x, y in unvisited:
            visited.append((x,y))
            if S[y-1][x] != '#' and (x, y-1) not in visited:
                un.append((x, y-1))

            if S[y+1][x] != '#' and (x, y+1) not in visited:
                if y+1 == H and x == W:
                    return dist
                un.append((x, y+1))

            if S[y][x-1] != '#' and (x-1, y) not in visited:
                un.append((x-1, y))

            if S[y][x+1] != '#' and (x+1, y) not in visited:
                if y == H and x+1 == W:
                    return dist
                un.append((x+1, y))
        unvisited = un

    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.insert(H+1,'#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

def bfs(H, W, S):
    visited = [[False]*(W+2) for _ in range(H+2)]
    visited[1][1] = True
    unvisited = deque([(1,1)])
    dist = 1
    while unvisited:
        dist += 1
        un = deque()
        for x, y in unvisited:
            for t1, t2 in zip([-1,+1,0,0],[0,0,-1,1]):
                dx = x + t1
                dy = y + t2
                if S[dy][dx] != '#' and not visited[dy][dx]:
                    if dy == H and dx == W:
                        return dist
                    un.append((dx,dy))
                    visited[dy][dx] = True
        unvisited = un

    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.append('#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import functools
import itertools
import math
from collections import deque

def bfs(H, W, S):
    cost = [[0]*(W+2) for _ in range(H+2)]
    visited = deque([])
    unvisited = deque([(1,1)])
    cost[1][1] = 1
    while unvisited:
        now = unvisited.popleft()
        visited.append(now)
        if S[now[1]-1][now[0]] != '#' and (now[0], now[1]-1) not in visited:
            cost[now[1]-1][now[0]] = cost[now[1]][now[0]] + 1
            unvisited.append((now[0], now[1]-1))
        if S[now[1]+1][now[0]] != '#' and (now[0], now[1]+1) not in visited:
            if now[1]+1 == H and now[0] == W:
                return cost[now[1]][now[0]] + 1
            cost[now[1]+1][now[0]] = cost[now[1]][now[0]] + 1
            unvisited.append((now[0], now[1]+1))
        if S[now[1]][now[0]-1] != '#' and (now[0]-1, now[1]) not in visited:
            cost[now[1]][now[0]-1] = cost[now[1]][now[0]] + 1
            unvisited.append((now[0]-1, now[1]))
        if S[now[1]][now[0]+1] != '#' and (now[0]+1, now[1]) not in visited:
            if now[1] == H and now[0]+1 == W:
                return cost[now[1]][now[0]] + 1
            cost[now[1]][now[0]+1] = cost[now[1]][now[0]] + 1
            unvisited.append((now[0]+1, now[1]))
    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.insert(H+1,'#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

def bfs(H, W, S):
    visited = [[False]*(W+2) for _ in range(H+2)]
    visited[1][1] = True
    unvisited = deque([(1,1)])
    dist = 1
    while unvisited:
        dist += 1
        un = deque()
        for x, y in unvisited:
            for t1, t2 in zip([-1,+1,0,0],[0,0,-1,1]):
                dx = x + t1
                dy = y + t2
                if S[dy][dx] != '#' and not visited[dy][dx]:
                    if dy == H and dx == W:
                        return dist
                    un.append((dx,dy))
                    visited[dy][dx] = True
        unvisited = un

    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.append('#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import functools
import itertools
import math

def bfs(H, W, S):
    cost = [[0]*(W+2) for _ in range(H+2)]
    visited = []
    unvisited = [(1,1)]
    cost[1][1] = 1
    while unvisited:
        now = unvisited[0]
        unvisited = unvisited[1:]
        visited.append(now)
        if S[now[1]-1][now[0]] != '#' and (now[0], now[1]-1) not in visited:
            cost[now[1]-1][now[0]] = cost[now[1]][now[0]] + 1
            unvisited.append((now[0], now[1]-1))
        if S[now[1]+1][now[0]] != '#' and (now[0], now[1]+1) not in visited:
            if now[1]+1 == H and now[0] == W:
                return cost[now[1]][now[0]] + 1
            cost[now[1]+1][now[0]] = cost[now[1]][now[0]] + 1
            unvisited.append((now[0], now[1]+1))
        if S[now[1]][now[0]-1] != '#' and (now[0]-1, now[1]) not in visited:
            cost[now[1]][now[0]-1] = cost[now[1]][now[0]] + 1
            unvisited.append((now[0]-1, now[1]))
        if S[now[1]][now[0]+1] != '#' and (now[0]+1, now[1]) not in visited:
            if now[1] == H and now[0]+1 == W:
                return cost[now[1]][now[0]] + 1
            cost[now[1]][now[0]+1] = cost[now[1]][now[0]] + 1
            unvisited.append((now[0]+1, now[1]))
    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.insert(H+1,'#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

def bfs(H, W, S):
    visited = [[False]*(W+2) for _ in range(H+2)]
    visited[1][1] = True
    unvisited = deque([(1,1)])
    dist = 1
    while unvisited:
        dist += 1
        un = deque()
        for x, y in unvisited:
            for t1, t2 in zip([-1,+1,0,0],[0,0,-1,1]):
                dx = x + t1
                dy = y + t2
                if S[dy][dx] != '#' and not visited[dy][dx]:
                    if dy == H and dx == W:
                        return dist
                    un.append((dx,dy))
                    visited[dy][dx] = True
        unvisited = un

    return -1

def main():
    H, W = map(int, input().split())
    S = ['#'+input()+'#' for _ in range(H)]
    S.insert(0,'#'*(W+2))
    S.append('#'*(W+2))
    step = bfs(H,W,S)
    white = sum([n.count('.') for n in S])
    if step == -1:
        print(step)
    else:
        print(white - step)

if __name__ == "__main__":
    main()