Buggy python submission (verdict: Time Limit Exceeded):
h,w=map(int,input().split())
maze=[["#" for i in range(2+w)]]
for _ in range(h):
    x=list(input())
    x.insert(0,"#")
    x.append("#")
    maze.append(x)
    
maze.append(["#" for i in range(2+w)])

#maze[i][j]=(i,j)番目の様子がどうなっているか。"#"の時地雷、"."の時oke

from collections import deque

d=deque()
d.append((1,1))





visited=[[False for i in range(w+2)] for j in range(h+2)]

dis=0
visited[1][1]=True

while visited[h][w]==False:
    e=deque()
    dis+=1
    while d:
    	x=d.popleft()
    	X,Y=x[0],x[1]
    	if visited[X-1][Y]==False and maze[X-1][Y]==".":
        	visited[X-1][Y]=True
        	e.append((X-1,Y))
    	if visited[X+1][Y]==False and maze[X+1][Y]==".":
        	visited[X+1][Y]=True
        	e.append((X+1,Y))
    	if visited[X][Y-1]==False and maze[X][Y-1]==".":
        	visited[X][Y-1]=True
        	e.append((X,Y-1))
    	if visited[X][Y+1]==False and maze[X][Y+1]==".":
        	visited[X][Y+1]=True
        	e.append((X,Y+1))
    if e:
      d=e
      e=deque()

    
dis=dis+1

cnt=0

#全体の白い領域を見る
for i in range(h+2):
    for j in range(2+w):
        if maze[i][j]==".":
            cnt+=1
        
        
print(cnt-dis)


Fixed submission:
from collections import deque
d=deque()
e=deque()
h,w=map(int,input().split())
field=[0 for i in range(h+2)]
field[0]=[True for i in range(w+2)]
distance=0
white=0
for i in range(h):
    s=input()
    a=[False for i in range(w+2)]
    a[0]=True
    for j in range(w):
        if s[j]=="#":
            a[j+1]=True
        else:
            white+=1

    a[w+1]=True
    field[i+1]=a
field[h+1]=[True for i in range(w+2)]
d.append((1,1))
ans=-1
while d:
    while d:
        some=d.popleft()
        index1,index2=some[0],some[1]
        if index1==h and index2==w:
            ans=distance
        if field[index1-1][index2]==False:
                field[index1-1][index2]=True
                e.append((index1-1,index2))
        if field[index1+1][index2]==False:
                field[index1+1][index2]=True
                e.append((index1+1,index2))
        if field[index1][index2+1]==False:
                field[index1][index2+1]=True
                e.append((index1,index2+1))
        if  field[index1][index2-1]==False:
                field[index1][index2-1]=True
                e.append((index1,index2-1))
    distance+=1
    d=e
    e=deque()
if ans>=0:
    print(white-ans-1)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
h,w=map(int,input().split())
maze=[["#" for i in range(2+w)]]
for _ in range(h):
    x=list(input())
    x.insert(0,"#")
    x.append("#")
    maze.append(x)
    
maze.append(["#" for i in range(2+w)])

#maze[i][j]=(i,j)番目の様子がどうなっているか。"#"の時地雷、"."の時oke

from collections import deque

d=deque()
d.append((1,1))





visited=[[False for i in range(w+2)] for j in range(h+2)]

dis=0
visited[1][1]=True

while visited[h][w]==False:
    e=deque()
    dis+=1
    while d:

    	x=d.popleft()

    
    
    	X,Y=x[0],x[1]
    	if visited[X-1][Y]==False and maze[X-1][Y]==".":
        	visited[X-1][Y]=True
        	e.append((X-1,Y))
    	if visited[X+1][Y]==False and maze[X+1][Y]==".":
        	visited[X+1][Y]=True
        	e.append((X+1,Y))
    	if visited[X][Y-1]==False and maze[X][Y-1]==".":
        	visited[X][Y-1]=True
        	e.append((X,Y-1))
    	if visited[X][Y+1]==False and maze[X][Y+1]==".":
        	visited[X][Y+1]=True
        	e.append((X,Y+1))
    if e:
      d=e
      e=deque()

    
dis=dis+1




cnt=0

#全体の白い領域を見る
for i in range(h+2):
    for j in range(2+w):
        if maze[i][j]==".":
            cnt+=1
        
        
print(cnt-dis)


Fixed submission:
from collections import deque
d=deque()
e=deque()
h,w=map(int,input().split())
field=[0 for i in range(h+2)]
field[0]=[True for i in range(w+2)]
distance=0
white=0
for i in range(h):
    s=input()
    a=[False for i in range(w+2)]
    a[0]=True
    for j in range(w):
        if s[j]=="#":
            a[j+1]=True
        else:
            white+=1

    a[w+1]=True
    field[i+1]=a
field[h+1]=[True for i in range(w+2)]
d.append((1,1))
ans=-1
while d:
    while d:
        some=d.popleft()
        index1,index2=some[0],some[1]
        if index1==h and index2==w:
            ans=distance
        if field[index1-1][index2]==False:
                field[index1-1][index2]=True
                e.append((index1-1,index2))
        if field[index1+1][index2]==False:
                field[index1+1][index2]=True
                e.append((index1+1,index2))
        if field[index1][index2+1]==False:
                field[index1][index2+1]=True
                e.append((index1,index2+1))
        if  field[index1][index2-1]==False:
                field[index1][index2-1]=True
                e.append((index1,index2-1))
    distance+=1
    d=e
    e=deque()
if ans>=0:
    print(white-ans-1)
else:
    print(-1)