Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue
H, W = map(int,input().split())
MAP = []
for _ in range(H):
    MAP.append(list(input()))
tx = [1,0,-1,0]
ty = [0,1,0,-1]
cnt = 0
for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            cnt += 1
q = Queue()
q.put([0,0,1])
flag = 1
while not q.empty():
    x, y, index = q.get()
    MAP[x][y] = '#'
    if x == H - 1 and y == W - 1:
        flag = 0
        cnt += index
        break
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx <= H-1 and 0 <= ny <= W-1 and MAP[nx][ny] == '.':
            q.put([nx,ny,index+1])
if flag:
    print(-1)
else:
    ans = H * W - cnt
    print(ans)

Fixed submission:
from queue import Queue
H, W = map(int,input().split())
MAP = []
for _ in range(H):
    MAP.append(list(input()))
tx = [1,0,-1,0]
ty = [0,1,0,-1]
cnt = 0
INF = 10 ** 8
for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            cnt += 1
d = [[INF for i in range(W)] for j in range(H)]
q = Queue()
q.put([0,0])
d[0][0] = 1
flag = 1
while not q.empty():
    x, y = q.get()
    if x == H - 1 and y == W - 1:
        flag = 0
        break
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx <= H-1 and 0 <= ny <= W-1 and MAP[nx][ny] == '.' and d[nx][ny] == INF:
            d[nx][ny] = d[x][y] + 1
            q.put([nx,ny])
if flag:
    print(-1)
else:
    ans = H * W - d[H-1][W-1] - cnt
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from queue import Queue
H, W = map(int,input().split())
MAP = []
for _ in range(H):
    MAP.append(list(input()))
tx = [1,0,-1,0]
ty = [0,1,0,-1]
cnt = 0
for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            cnt += 1
q = Queue()
q.put([0,0,1])
while True:
    x, y, index = q.get()
    MAP[x][y] = '#'
    if x == H - 1 and y == W - 1:
        cnt += index
        break
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx <= H-1 and 0 <= ny <= W-1 and MAP[nx][ny] == '.':
            q.put([nx,ny,index+1])

ans = H * W - cnt
print(ans)

Fixed submission:
from queue import Queue
H, W = map(int,input().split())
MAP = []
for _ in range(H):
    MAP.append(list(input()))
tx = [1,0,-1,0]
ty = [0,1,0,-1]
cnt = 0
INF = 10 ** 8
for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            cnt += 1
d = [[INF for i in range(W)] for j in range(H)]
q = Queue()
q.put([0,0])
d[0][0] = 1
flag = 1
while not q.empty():
    x, y = q.get()
    if x == H - 1 and y == W - 1:
        flag = 0
        break
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx <= H-1 and 0 <= ny <= W-1 and MAP[nx][ny] == '.' and d[nx][ny] == INF:
            d[nx][ny] = d[x][y] + 1
            q.put([nx,ny])
if flag:
    print(-1)
else:
    ans = H * W - d[H-1][W-1] - cnt
    print(ans)

Buggy python submission (verdict: Runtime Error):
H, W = map(int,input().split())
MAP = []
INF = 10 ** 8
for _ in range(H):
    MAP.append(list(input()))
tx = [1,0,-1,0]
ty = [0,-1,0,1]
def dfs(x, y, index):
    if x < 0 or H <= x or y < 0 or W <= y:
        return INF
    if MAP[x][y] == '#':
        return INF
    if x == H - 1 and y == W - 1:
        return index
    ret = INF
    MAP[x][y] = '#'
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        ret = min(ret, dfs(nx, ny, index+1))
    return ret
cnt = 0
for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            cnt += 1
cnt += dfs(0,0,1)
ans = H * W - cnt
print(ans)

Fixed submission:
from queue import Queue
H, W = map(int,input().split())
MAP = []
for _ in range(H):
    MAP.append(list(input()))
tx = [1,0,-1,0]
ty = [0,1,0,-1]
cnt = 0
INF = 10 ** 8
for i in range(H):
    for j in range(W):
        if MAP[i][j] == '#':
            cnt += 1
d = [[INF for i in range(W)] for j in range(H)]
q = Queue()
q.put([0,0])
d[0][0] = 1
flag = 1
while not q.empty():
    x, y = q.get()
    if x == H - 1 and y == W - 1:
        flag = 0
        break
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx <= H-1 and 0 <= ny <= W-1 and MAP[nx][ny] == '.' and d[nx][ny] == INF:
            d[nx][ny] = d[x][y] + 1
            q.put([nx,ny])
if flag:
    print(-1)
else:
    ans = H * W - d[H-1][W-1] - cnt
    print(ans)