Buggy python submission (verdict: Time Limit Exceeded):
import heapq

H, W = map(int, input().split())
board = []
goal_dst = [[float("inf") for _ in range(W)] for _ in range(H)]
no = 0

for _ in range(H):
	board.append(list(map(str, input())))
	no += board[-1].count("#")
	
# bfsで最短路を求める
Q = [[1, 0, 0]]
juuji = [[-1, 0], [1, 0], [0, -1], [0, 1]]

while Q:
	cur = heapq.heappop(Q)
	dist = cur[0]
	if goal_dst[cur[1]][cur[2]] < dist or goal_dst[-1][-1] < dist: continue
	goal_dst[cur[1]][cur[2]] = dist
	for move in juuji:
		new_y = cur[1] + move[0]
		new_x = cur[2] + move[1]
		if not (0 <= new_x and new_x <= W - 1 and 0 <= new_y and new_y <= H - 1): continue 
		if board[new_y][new_x] == ".":
			heapq.heappush(Q, [dist + 1, new_y, new_x])
			
if goal_dst[-1][-1] == float("inf"):
	print(-1)
else:
	print(H * W - goal_dst[-1][-1] - no)

Fixed submission:
from heapq import heappush, heappop
H, W = map(int, input().split())
s = [input() for _ in range(H)]

dist = [[float('inf')] * W for _ in range(H)]
q = [(0, 0, 0)]
while q :
    cost, cx, cy = heappop(q)
    if cost >= dist[cy][cx] :
        continue
    dist[cy][cx] = cost
        
    for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)) :
        if 0 <= cx + dx < W and 0 <= cy + dy < H and s[cy+dy][cx+dx] != '#' :
            heappush(q, (cost+1, cx+dx, cy+dy))
            
if dist[H-1][W-1] == float('inf') :
    print(-1)
else :
    ret = 0
    for s_ in s :
        ret += s_.count('#')
    print(H * W - ret - dist[H-1][W-1] - 1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

H, W = map(int, input().split())
board = []
visited = [[False for _ in range(W)] for _ in range(H)]
goal_dst = [[float("inf") for _ in range(W)] for _ in range(H)]
no = 0

for _ in range(H):
	board.append(list(map(str, input())))
	no += board[-1].count("#")
	
# bfsで最短路を求める
Q = [[1, 0, 0]]
juuji = [[-1, 0], [1, 0], [0, -1], [0, 1]]

while Q:
	cur = heapq.heappop(Q)
	visited[cur[1]][cur[2]] = True
	dist = cur[0]
	if goal_dst[cur[1]][cur[2]] < dist or goal_dst[-1][-1] < dist: continue
	goal_dst[cur[1]][cur[2]] = dist
	for move in juuji:
		new_y = cur[1] + move[0]
		new_x = cur[2] + move[1]
		if not (0 <= new_x and new_x <= W - 1 and 0 <= new_y and new_y <= H - 1): continue 
		if board[new_y][new_x] == "." and visited[new_y][new_x] == False:
			heapq.heappush(Q, [dist + 1, new_y, new_x])
			
if goal_dst[-1][-1] == float("inf"):
	print(-1)
else:
	print(H * W - goal_dst[-1][-1] - no)

Fixed submission:
from heapq import heappush, heappop
H, W = map(int, input().split())
s = [input() for _ in range(H)]

dist = [[float('inf')] * W for _ in range(H)]
q = [(0, 0, 0)]
while q :
    cost, cx, cy = heappop(q)
    if cost >= dist[cy][cx] :
        continue
    dist[cy][cx] = cost
        
    for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)) :
        if 0 <= cx + dx < W and 0 <= cy + dy < H and s[cy+dy][cx+dx] != '#' :
            heappush(q, (cost+1, cx+dx, cy+dy))
            
if dist[H-1][W-1] == float('inf') :
    print(-1)
else :
    ret = 0
    for s_ in s :
        ret += s_.count('#')
    print(H * W - ret - dist[H-1][W-1] - 1)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

H, W = map(int, input().split())
board = []
visited = [[False for _ in range(W)] for _ in range(H)]
goal_dst = [[float("inf") for _ in range(W)] for _ in range(H)]
no = 0

for _ in range(H):
	board.append(list(map(str, input())))
	no += board[-1].count("#")
	
# bfsで最短路を求める
Q = [[0, 0, 1]]
juuji = [[-1, 0], [1, 0], [0, -1], [0, 1]]

while Q:
	cur = heapq.heappop(Q)
	visited[cur[0]][cur[1]] = True
	dist = cur[2]
	if goal_dst[cur[0]][cur[1]] < dist or goal_dst[-1][-1] < dist: continue
	goal_dst[cur[0]][cur[1]] = dist
	for move in juuji:
		new_y = cur[0] + move[0]
		new_x = cur[1] + move[1]
		if not (0 <= new_x and new_x <= W - 1 and 0 <= new_y and new_y <= H - 1): continue 
		if board[new_y][new_x] == "." and visited[new_y][new_x] == False:
			heapq.heappush(Q, [new_y, new_x, dist + 1])
			
if goal_dst[-1][-1] == float("inf"):
	print(-1)
else:
	print(H * W - goal_dst[-1][-1] - no)

Fixed submission:
from heapq import heappush, heappop
H, W = map(int, input().split())
s = [input() for _ in range(H)]

dist = [[float('inf')] * W for _ in range(H)]
q = [(0, 0, 0)]
while q :
    cost, cx, cy = heappop(q)
    if cost >= dist[cy][cx] :
        continue
    dist[cy][cx] = cost
        
    for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)) :
        if 0 <= cx + dx < W and 0 <= cy + dy < H and s[cy+dy][cx+dx] != '#' :
            heappush(q, (cost+1, cx+dx, cy+dy))
            
if dist[H-1][W-1] == float('inf') :
    print(-1)
else :
    ret = 0
    for s_ in s :
        ret += s_.count('#')
    print(H * W - ret - dist[H-1][W-1] - 1)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
board = []
visited = [[False for _ in range(W)] for _ in range(H)]
goal_dst = [[float("inf") for _ in range(W)] for _ in range(H)]
no = 0

for _ in range(H):
	board.append(list(map(str, input())))
	no += board[-1].count("#")
	
# bfsで最短路を求める
Q = [[0, 0, 1]]
juuji = [[-1, 0], [1, 0], [0, -1], [0, 1]]

while Q:
	cur = Q.pop(0)
	visited[cur[0]][cur[1]] = True
	dist = cur[2]
	if goal_dst[cur[0]][cur[1]] < dist: continue
	goal_dst[cur[0]][cur[1]] = dist
	for move in juuji:
		new_y = cur[0] + move[0]
		new_x = cur[1] + move[1]
		if not (0 <= new_x and new_x <= W - 1 and 0 <= new_y and new_y <= H - 1): continue 
		if board[new_y][new_x] == "." and visited[new_y][new_x] == False:
			Q.append([new_y, new_x, dist + 1])
			
if goal_dst[-1][-1] == float("inf"):
	print(-1)
else:
	print(H * W - goal_dst[-1][-1] - no)

Fixed submission:
from heapq import heappush, heappop
H, W = map(int, input().split())
s = [input() for _ in range(H)]

dist = [[float('inf')] * W for _ in range(H)]
q = [(0, 0, 0)]
while q :
    cost, cx, cy = heappop(q)
    if cost >= dist[cy][cx] :
        continue
    dist[cy][cx] = cost
        
    for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)) :
        if 0 <= cx + dx < W and 0 <= cy + dy < H and s[cy+dy][cx+dx] != '#' :
            heappush(q, (cost+1, cx+dx, cy+dy))
            
if dist[H-1][W-1] == float('inf') :
    print(-1)
else :
    ret = 0
    for s_ in s :
        ret += s_.count('#')
    print(H * W - ret - dist[H-1][W-1] - 1)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
board = []
visited = [[False for _ in range(W)] for _ in range(H)]
goal_dst = float("inf")
no = 0

for _ in range(H):
	board.append(list(map(str, input())))
	no += board[-1].count("#")
	
# bfsで最短路を求める
Q = [[0, 0, 1]]
juuji = [[-1, 0], [1, 0], [0, -1], [0, 1]]

while Q:
	cur = Q.pop(0)
	visited[cur[0]][cur[1]] = True
	dist = cur[2]
	if cur[0] == H - 1 and cur[1] == W - 1:
		goal_dst = min(goal_dst, dist)
	if goal_dst < dist: continue
	for move in juuji:
		new_y = cur[0] + move[0]
		new_x = cur[1] + move[1]
		if not (0 <= new_x and new_x <= W - 1 and 0 <= new_y and new_y <= H - 1): continue 
		if board[new_y][new_x] == "." and visited[new_y][new_x] == False:
			Q.append([new_y, new_x, dist + 1])
			
if goal_dst == float("inf"):
	print(-1)
else:
	print(H * W - goal_dst - no)

Fixed submission:
from heapq import heappush, heappop
H, W = map(int, input().split())
s = [input() for _ in range(H)]

dist = [[float('inf')] * W for _ in range(H)]
q = [(0, 0, 0)]
while q :
    cost, cx, cy = heappop(q)
    if cost >= dist[cy][cx] :
        continue
    dist[cy][cx] = cost
        
    for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)) :
        if 0 <= cx + dx < W and 0 <= cy + dy < H and s[cy+dy][cx+dx] != '#' :
            heappush(q, (cost+1, cx+dx, cy+dy))
            
if dist[H-1][W-1] == float('inf') :
    print(-1)
else :
    ret = 0
    for s_ in s :
        ret += s_.count('#')
    print(H * W - ret - dist[H-1][W-1] - 1)