Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
h,w=map(int,input().split())
field=[list(input()) for _ in range(h)]
count=0
queue=deque([[0,0,0]])
for i in range(h):
    for j in range(w):
        if field[i][j]=='.':
            count+=1
def bfs(x,y,k):
    if (not 0<=x<h) or (not 0<=y<w):
        return
    if field[x][y]=='#':
        return
    if visit[x][y]==1:
        return
    queue.append([x,y,k+1])
visit=[[0]*w for _ in range(h)]
while queue:
    x,y,k=queue.popleft()
#    print(queue)
#    print(field)
    if x==h-1 and y==w-1:
        print(count-k-1)
        sys.exit()
    
    visit[x][y]=1
    bfs(x+1,y,k)
    bfs(x-1,y,k)
    bfs(x,y-1,k)
    bfs(x,y+1,k)
if visit[h-1][w-1]==0:
    print(-1)
else:
    print(count-k-1)

Fixed submission:
from collections import deque
h,w=map(int,input().split())
field=[list(input()) for _ in range(h)]
cost=[[float('inf')]*w for _ in range(h)]
cost[0][0]=1
queue=deque([[0,0]])
count=0
for i in range(h):
    for j in range(w):
        if field[i][j]=='.':
            count+=1
def bfs():
    while queue:
        x,y=queue.popleft()
        for dx,dy in [[1,0],[-1,0],[0,1],[0,-1]]:
            if 0<=x+dx<h and 0<=y+dy<w:
                if field[x+dx][y+dy]=='.' and cost[x+dx][y+dy]==float('inf'):
                    cost[x+dx][y+dy]=cost[x][y]+1
                    queue.append([x+dx,y+dy])
bfs()
#print(cost)
if cost[-1][-1]<float('inf'):
    print(count-cost[-1][-1])
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
h,w=map(int,input().split())
field=[list(input()) for _ in range(h)]
count=0
queue=deque([[0,0,0]])
for i in range(h):
    for j in range(w):
        if field[i][j]=='.':
            count+=1
def bfs(x,y,k):
    if (not 0<=x<h) or (not 0<=y<w):
        return
    if field[x][y]=='#':
        return
    queue.append([x,y,k+1])
    
while True:
    if len(queue)==0:
        break
    x,y,k=queue.popleft()
#    print(queue)
#    print(field)
    if x==h-1 and y==w-1:
        print(count-k-1)
        sys.exit()
    
    field[x][y]='#'
    bfs(x+1,y,k)
    bfs(x-1,y,k)
    bfs(x,y-1,k)
    bfs(x,y+1,k)
print(-1)

Fixed submission:
from collections import deque
h,w=map(int,input().split())
field=[list(input()) for _ in range(h)]
cost=[[float('inf')]*w for _ in range(h)]
cost[0][0]=1
queue=deque([[0,0]])
count=0
for i in range(h):
    for j in range(w):
        if field[i][j]=='.':
            count+=1
def bfs():
    while queue:
        x,y=queue.popleft()
        for dx,dy in [[1,0],[-1,0],[0,1],[0,-1]]:
            if 0<=x+dx<h and 0<=y+dy<w:
                if field[x+dx][y+dy]=='.' and cost[x+dx][y+dy]==float('inf'):
                    cost[x+dx][y+dy]=cost[x][y]+1
                    queue.append([x+dx,y+dy])
bfs()
#print(cost)
if cost[-1][-1]<float('inf'):
    print(count-cost[-1][-1])
else:
    print(-1)

Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys
h,w=map(int,input().split())
field=[list(input()) for _ in range(h)]
count=0
queue=deque([[0,0,0]])
for i in range(h):
    for j in range(w):
        if field[i][j]=='.':
            count+=1
def bfs(x,y,k):
    if (not 0<=x<h) or (not 0<=y<w):
        return
    if field[x][y]=='#':
        return
    queue.append([x,y,k+1])
#    print(queue)
#    print(field)
while True:
    x,y,k=queue.popleft()
    if x==h-1 and y==w-1:
        print(count-k-1)
        sys.exit()
    field[x][y]='#'
    bfs(x+1,y,k)
    bfs(x-1,y,k)
    bfs(x,y-1,k)
    bfs(x,y+1,k)
print(-1)

Fixed submission:
from collections import deque
h,w=map(int,input().split())
field=[list(input()) for _ in range(h)]
cost=[[float('inf')]*w for _ in range(h)]
cost[0][0]=1
queue=deque([[0,0]])
count=0
for i in range(h):
    for j in range(w):
        if field[i][j]=='.':
            count+=1
def bfs():
    while queue:
        x,y=queue.popleft()
        for dx,dy in [[1,0],[-1,0],[0,1],[0,-1]]:
            if 0<=x+dx<h and 0<=y+dy<w:
                if field[x+dx][y+dy]=='.' and cost[x+dx][y+dy]==float('inf'):
                    cost[x+dx][y+dy]=cost[x][y]+1
                    queue.append([x+dx,y+dy])
bfs()
#print(cost)
if cost[-1][-1]<float('inf'):
    print(count-cost[-1][-1])
else:
    print(-1)