Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
S = []
for _ in range(H):
    s = input()
    S.append(s)
    
import copy
maze = copy.deepcopy(S)

import queue

INT_MAX = 2**63-1
stayed = [[INT_MAX] * W for _ in range(H)]

def bfs():
    gole = (W-1, H-1)
    q = queue.Queue()
    q.put((
        0, # current x
        0, # current y
        0, # count
    ))

    while q:
        cur_x, cur_y, cnt = q.get()
        if (cur_x, cur_y) == gole:
            return cnt
        
        next_pos = [
            (cur_x, cur_y-1),
            (cur_x, cur_y+1),
            (cur_x-1, cur_y),
            (cur_x+1, cur_y)
        ]
        
        for x, y in next_pos:
            if x == -1 or y == -1 or x == W or y == H:
                continue
            if S[y][x] == '#':
                continue
            if stayed[y][x] > cnt+1:
                stayed[y][x] = cnt+1
                q.put((x, y, cnt+1))
    return -1
            

shortest_path_len = bfs()

if shortest_path_len != -1:
    cnt_dot = sum([s.count('.') for s in maze])


    ans = cnt_dot - shortest_path_len - 1
    print(ans)
else:
    print(-1)
        
        
        

Fixed submission:
H, W = map(int, input().split())
S = []
for _ in range(H):
    s = input()
    S.append(s)
    
import copy
maze = copy.deepcopy(S)

import queue

INT_MAX = 2**63-1
stayed = [[INT_MAX] * W for _ in range(H)]

def bfs():
    gole = (W-1, H-1)
    q = queue.Queue()
    q.put((
        0, # current x
        0, # current y
        0, # count
    ))

    while not q.empty():
        cur_x, cur_y, cnt = q.get()
        if (cur_x, cur_y) == gole:
            return cnt
        next_pos = [
            (cur_x, cur_y-1),
            (cur_x, cur_y+1),
            (cur_x-1, cur_y),
            (cur_x+1, cur_y)
        ]
        
        for x, y in next_pos:
            if x == -1 or y == -1 or x == W or y == H:
                continue
            if S[y][x] == '#':
                continue
            if stayed[y][x] > cnt+1:
                stayed[y][x] = cnt+1
                q.put((x, y, cnt+1))
                
        #print(stayed)
    return -1
            

shortest_path_len = bfs()

if shortest_path_len != -1:
    cnt_dot = sum([s.count('.') for s in maze])


    ans = cnt_dot - shortest_path_len - 1
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
S = []
for _ in range(H):
    s = input()
    S.append(s)
    
import copy
maze = copy.deepcopy(S)

import queue

INT_MAX = 2**63-1
stayed = [[INT_MAX] * W for _ in range(H)]

def bfs():
    gole = (W-1, H-1)
    q = queue.Queue()
    q.put((
        0, # current x
        0, # current y
        0, # count
    ))

    while q:
        cur_x, cur_y, cnt = q.get()
        if (cur_x, cur_y) == gole:
            return cnt
        
        next_pos = [
            (cur_x, cur_y-1),
            (cur_x, cur_y+1),
            (cur_x-1, cur_y),
            (cur_x+1, cur_y)
        ]
        
        for x, y in next_pos:
            if x == -1 or y == -1 or x == W or y == H:
                continue
            if S[y][x] == '#':
                continue
            if stayed[y][x] > cnt+1:
                stayed[y][x] = cnt+1
                q.put((x, y, cnt+1))
            

shortest_path_len = bfs()

cnt_dot = sum([s.count('.') for s in maze])


#print(shortest_path_len)
#print(cnt_dot)

ans = cnt_dot - shortest_path_len - 1
print(ans)
#print(stayed)
        
        
        

Fixed submission:
H, W = map(int, input().split())
S = []
for _ in range(H):
    s = input()
    S.append(s)
    
import copy
maze = copy.deepcopy(S)

import queue

INT_MAX = 2**63-1
stayed = [[INT_MAX] * W for _ in range(H)]

def bfs():
    gole = (W-1, H-1)
    q = queue.Queue()
    q.put((
        0, # current x
        0, # current y
        0, # count
    ))

    while not q.empty():
        cur_x, cur_y, cnt = q.get()
        if (cur_x, cur_y) == gole:
            return cnt
        next_pos = [
            (cur_x, cur_y-1),
            (cur_x, cur_y+1),
            (cur_x-1, cur_y),
            (cur_x+1, cur_y)
        ]
        
        for x, y in next_pos:
            if x == -1 or y == -1 or x == W or y == H:
                continue
            if S[y][x] == '#':
                continue
            if stayed[y][x] > cnt+1:
                stayed[y][x] = cnt+1
                q.put((x, y, cnt+1))
                
        #print(stayed)
    return -1
            

shortest_path_len = bfs()

if shortest_path_len != -1:
    cnt_dot = sum([s.count('.') for s in maze])


    ans = cnt_dot - shortest_path_len - 1
    print(ans)
else:
    print(-1)