Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
 
h,w=[int(x) for x in input().rstrip().split()]
l=[[s for s in input()]for i in range(h)]

black=sum(s.count("#") for s in l)
dist=[[False for j in range(w)] for i in range(h)]
done=[[float('inf') for j in range(w)] for i in range(h)]
move=[[1,0],[-1,0],[0,1],[0,-1]]

def bfs():
  stack=deque([[0,0]])
  done[0][0]=0
  while(stack):
    x,y=stack.popleft()
    dist[y][x]=True
    
    for dx,dy in move:
      mx,my=x+dx,y+dy
      if (0<=my<=h-1) and (0<=mx<=w-1) and l[my][mx]=="." and dist[my][mx]==False:
        done[my][mx]=done[y][x]+1
        stack.append([mx,my])
  return done[h-1][w-1]
 
if bfs()!=float('inf'):print(h*w-done[h-1][w-1]-black-1)
else:print(-1)


Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
 
h,w=[int(x) for x in input().rstrip().split()]
l=[[s for s in input()]for i in range(h)]

black=sum(s.count("#") for s in l)
dist=[[False for j in range(w)] for i in range(h)]
done=[[float('inf') for j in range(w)] for i in range(h)]
move=[[1,0],[-1,0],[0,1],[0,-1]]

def bfs():
  stack=deque([[0,0]])
  done[0][0]=0
  while(stack):
    x,y=stack.popleft()
    dist[y][x]=True
    
    for dx,dy in move:
      mx,my=x+dx,y+dy
      if (0<=my<=h-1) and (0<=mx<=w-1) and l[my][mx]=="." and dist[my][mx]==False:
        done[my][mx]=done[y][x]+1
        stack.append([mx,my])
  return done[h-1][w-1]
 
if bfs()!=float('inf'):print(h*w-done[h-1][w-1]-black-1)
else:print(-1)


Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
 
h,w=[int(x) for x in input().rstrip().split()]
l=[[s for s in input()]for i in range(h)]

black=sum(s.count("#") for s in l)
dist=[[False for j in range(w)] for i in range(h)]
done=[[float('inf') for j in range(w)] for i in range(h)]
move=[[1,0],[-1,0],[0,1],[0,-1]]

def bfs():
  stack=deque([[0,0]])
  done[0][0]=0
  while(stack):
    x,y=stack.popleft()
    dist[y][x]=True

    if y==h-1 and x==w-1:
        return done[h-1][w-1]

    for dx,dy in move:
      mx,my=x+dx,y+dy
      if (0<=my<=h-1) and (0<=mx<=w-1) and l[my][mx]=="." and dist[my][mx]==False:
        done[my][mx]=done[y][x]+1
        stack.append([mx,my])
  return done[h-1][w-1]
 
if bfs()!=float('inf'):print(h*w-done[h-1][w-1]-black-1)
else:print(-1)


Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[[s for s in input()]for i in range(h)]

black=sum(s.count("#") for s in l)
dist=[[False for j in range(w)] for i in range(h)]
done=[[float('inf') for j in range(w)] for i in range(h)]
move=[[1,0],[-1,0],[0,1],[0,-1]]

def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    dist[y][x]=True

    if y==h-1 and x==w-1:
        return done[h-1][w-1]

    for dx,dy in move:
      mx,my=x+dx,y+dy
      if (0<=my<=h-1) and (0<=mx<=w-1) and l[my][mx]=="." and dist[my][mx]==False:
        done[my][mx]=done[y][x]+1
        stack.append([mx,my])
  return done[h-1][w-1]
 
if bfs()!=float('inf'):print(h*w-done[h-1][w-1]-black-1)
else:print(-1)


Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)

black=sum(s.count("#") for s in l)
dist=[[False for j in range(w)] for i in range(h)]
done=[[float('inf') for j in range(w)] for i in range(h)]
move=[[1,0],[-1,0],[0,1],[0,-1]]

def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    dist[y][x]=True
   
    for dx,dy in move:
      mx,my=x+dx,y+dy
      if not(0<=my<=h-1) or not(0<=mx<=w-1):
        continue
      if l[my][mx]=="#" or dist[my][mx]==True:
        continue
      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
    
      if my==h-1 and mx==w-1:
        return True
  return False
 
if bfs()==1:
  print(h*w-done[h-1][w-1]-black-1)
else:
  print(-1)


Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
 
black+=sum(map(lambda x:x.count('#'),l))
 
dist=[[False]*w for i in range(h)]
done=[[float('inf')]*w for i in range(h)]
 

move=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    dist[y][x]=True
   
    for dx,dy in move:
      mx=x+dx
      my=y+dy

      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or dist[my][mx]==True:
        continue
 
      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
    
      if my==h-1 and mx==w-1:
        return True

  return False
 
if __name__=="__main__":
  if bfs()==1:
    print(h*w-done[h-1][w-1]-black-1)
  else:
    print(-1)


Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

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

h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)

black+=sum(map(lambda x:x.count('#'),l))

dist=[[False]*w for i in range(h)]


def bfs():
  cnt=1
  stack=[[0,0,cnt]]

  while(stack):
    x,y,cnt=stack.pop(0)
    dist[y][x]=True
    
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]

    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or dist[my][mx]==True:
        continue

      
      stack.append([mx,my,cnt+1])
    
      if my==h-1 and mx==w-1:
        print(h*w-cnt-black-1)
        sys.exit()
  print(-1)

if __name__=="__main__":
  bfs()

Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

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

h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)

black+=sum(map(lambda x:x.count('#'),l))

dist=[[False]*w for i in range(h)]
done=[[float('inf')]*w for i in range(h)]

cnt=0
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    dist[y][x]=True
    
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]

    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or dist[my][mx]==True:
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
    
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)

if __name__=="__main__":
  bfs()

Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

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

h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=s.count("#")

dist=[[False]*w for i in range(h)]
done=[[float('inf')]*w for i in range(h)]

cnt=0
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    dist[y][x]=True
    
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]

    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or dist[my][mx]==True:
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
    
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)

if __name__=="__main__":
  bfs()

Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

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

h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  for j in s:
    if j=="#":
      black+=1

dist=[[False]*w for i in range(h)]
done=[[float('inf')]*w for i in range(h)]

cnt=0
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    dist[y][x]=True
    
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]

    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or dist[my][mx]==True:
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
    
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)


if __name__=="__main__":
  bfs()




Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()

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

h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  for j in s:
    if j=="#":
      black+=1

dist=[[False]*w for i in range(h)]
done=[[float('inf')]*w for i in range(h)]

cnt=0
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    dist[y][x]=True
    
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]

    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or dist[my][mx]==True:
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
    
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)


if __name__=="__main__":
  bfs()




Fixed submission:
import sys
 
h,w=[int(x) for x in input().rstrip().split()]
l=[]
black=0
for i in range(h):
  s=[x for x in input()]
  l.append(s)
  black+=sum(j.count("#") for j in s)
 
done=[[float('inf')]*w for i in range(h)]
def bfs():
  stack=[[0,0]]
  done[0][0]=0
  while(stack):
    x,y=stack.pop(0)
    move=[[x+1,y],[x-1,y],[x,y+1],[x,y-1]]
 
    for mx,my in move:
      if not(0<=my<=h-1) or not(0<=mx<=w-1) or l[my][mx]=="#" or done[my][mx]!=float('inf'):
        continue

      done[my][mx]=done[y][x]+1
      stack.append([mx,my])
      
      if my==h-1 and mx==w-1:
        print(h*w-done[my][mx]-black-1)
        sys.exit()
  print(-1)
 
if __name__=="__main__":
  bfs()