Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, Counter
import sys
H, W = map(int, input().split())
count = 0
grid = ['s' for h in range(H)]
for h in range(H):
    grid[h] = list(input())
    tmp_count = Counter(grid[h])['#']
    if tmp_count == W:
        print(-1)
        sys.exit()
    count += tmp_count

deq = deque([(0, 0)])
visited = [[False for w in range(W)] for h in range(H)]
visited[0][0] = True
route = 0
while deq:
    route += 1
    for _ in range(len(deq)):
        coord = deq.popleft()
        h, w = coord
        visited[h][w] = True
        if coord == (H - 1, W - 1):
            print(H * W - route - count)
            sys.exit()
        if h < H - 1:
            if grid[h + 1][w] != '#' and not visited[h + 1][w]:
                deq.append((h + 1, w))
        if w < W - 1:
            if grid[h][w + 1] != '#' and not visited[h][w + 1]:
                deq.append((h, w + 1))
        if h > 0:
            if grid[h - 1][w] != '#' and not visited[h - 1][w]:
                deq.append((h - 1, w))
        if w > 0:
            if grid[h][w - 1] != '#' and not visited[h][w - 1]:
                deq.append((h, w - 1))

print(-1)




Fixed submission:
from collections import deque, Counter
import sys
H, W = map(int, input().split())
count = 0
grid = ['s' for h in range(H)]
for h in range(H):
    grid[h] = list(input())
    tmp_count = Counter(grid[h])['#']
    if tmp_count == W:
        print(-1)
        sys.exit()
    count += tmp_count

deq = deque([(0, 0)])
visited = [[-1 for w in range(W)] for h in range(H)]
visited[0][0] = 0
route = 0
while deq:
    for _ in range(len(deq)):
        h, w = deq.popleft()
        #print(h, w)
        if (h, w) == (H - 1, W - 1):
            print(H * W - visited[h][w] - 1 - count)
            sys.exit()
        for j, k in ([-1, 0], [0, -1], [0, 1], [1, 0]):
            new_h, new_w = h + j, w + k
            if 0 <= new_h <= H - 1 and 0 <= new_w <= W - 1:
                if grid[new_h][new_w] != '#' and visited[new_h][new_w] == -1:
                    visited[new_h][new_w] = visited[h][w] + 1
                    deq.append((new_h, new_w))
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, Counter
import sys
H, W = map(int, input().split())
count = 0
grid = ['s' for h in range(H)]
for h in range(H):
    grid[h] = list(input())
    count += Counter(grid[h])['#']

deq = deque([(0, 0)])
visited = [[False for w in range(W)] for h in range(H)]
visited[0][0] = True
route = 0
while deq:
    route += 1
    for _ in range(len(deq)):
        coord = deq.popleft()
        h, w = coord
        visited[h][w] = True
        if coord == (H - 1, W - 1):
            print(H * W - route - count)
            sys.exit()
        if h < H - 1:
            if grid[h + 1][w] != '#' and not visited[h + 1][w]:
                deq.append((h + 1, w))
        if w < W - 1:
            if grid[h][w + 1] != '#' and not visited[h][w + 1]:
                deq.append((h, w + 1))
        if h > 0:
            if grid[h - 1][w] != '#' and not visited[h - 1][w]:
                deq.append((h - 1, w))
        if w > 0:
            if grid[h][w - 1] != '#' and not visited[h][w - 1]:
                deq.append((h, w - 1))

print(-1)




Fixed submission:
from collections import deque, Counter
import sys
H, W = map(int, input().split())
count = 0
grid = ['s' for h in range(H)]
for h in range(H):
    grid[h] = list(input())
    tmp_count = Counter(grid[h])['#']
    if tmp_count == W:
        print(-1)
        sys.exit()
    count += tmp_count

deq = deque([(0, 0)])
visited = [[-1 for w in range(W)] for h in range(H)]
visited[0][0] = 0
route = 0
while deq:
    for _ in range(len(deq)):
        h, w = deq.popleft()
        #print(h, w)
        if (h, w) == (H - 1, W - 1):
            print(H * W - visited[h][w] - 1 - count)
            sys.exit()
        for j, k in ([-1, 0], [0, -1], [0, 1], [1, 0]):
            new_h, new_w = h + j, w + k
            if 0 <= new_h <= H - 1 and 0 <= new_w <= W - 1:
                if grid[new_h][new_w] != '#' and visited[new_h][new_w] == -1:
                    visited[new_h][new_w] = visited[h][w] + 1
                    deq.append((new_h, new_w))
print(-1)