Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy

H,W = map(int,input().split())
s = [list(input()) for _ in range(H)]
black=0

for tate in s:
  for yoko in tate:
    if yoko=="#":
      black += 1

def search(y,x,H,W,s,zumi):
  child=[]
      
  if y<H-1:
    if s[y+1][x]==".":
      if [y+1,x] not in zumi:
        child.append([y+1,x])
  if 0<y:
    if s[y-1][x]==".":
      if [y-1,x] not in zumi:
        child.append([y-1,x])
  if 0<x:
    if s[y][x-1]==".":
      if [y,x-1] not in zumi:
        child.append([y,x-1])
  if x<W-1:
    if s[y][x+1]==".":
      if [y,x+1] not in zumi:
        child.append([y,x+1])
  return child

def dfs(y,x,cnt,H,W,s,zumi):
  node_to_visit=search(y,x,H,W,s,zumi)
  zumi=deepcopy(node_to_visit)
  flag=True
  
  while flag:
    cnt += 1
    tmp=[]
    tmp2=[]
    #print(node_to_visit)
    zumi += node_to_visit
    for next_point in node_to_visit:
      t=next_point[0] #y。たて
      u=next_point[1] #x。よこ
      tmp=search(t,u,H,W,s,zumi)
      tmp2 += tmp
    node_to_visit=deepcopy(tmp2)
    if [H-1,W-1] in node_to_visit:
      white=cnt
      flag=False
      break     
  ans=H*W-white-black-2
  #print(white,black,ans)
  print(ans)

zumi=[]
dfs(0,0,0,H,W,s,zumi)

Fixed submission:
from collections import deque

H,W = map(int,input().split())
s = [list(input()) for _ in range(H)]
c = [[-1 for _ in range(W)] for _ in range(H)]

black=0
for tate in s:
  for yoko in tate:
    if yoko=="#":
      black += 1

def search(y,x,H,W,c,i):
  child=[]      
  if y<H-1:
    if s[y+1][x]=="." and c[y+1][x]==-1:
      c[y+1][x]=i
      child.append([y+1,x])
  if 0<y:
    if s[y-1][x]=="." and c[y-1][x]==-1:
      c[y-1][x]=i
      child.append([y-1,x])
  if 0<x:
    if s[y][x-1]=="." and c[y][x-1]==-1:
      c[y][x-1]=i
      child.append([y,x-1])
  if x<W-1:
    if s[y][x+1]=="." and c[y][x+1]==-1:
      c[y][x+1]=i
      child.append([y,x+1])
  return child

def dfs(y,x,c):
  visited=[[y,x]]
  c[y][x]=0
  i=1
  
  while visited:
    tmp=[]
    for a in visited:
      p=search(a[0],a[1],H,W,c,i)  #listが戻る
      tmp += p
    visited=tmp
    i += 1

dfs(0,0,c)
a=c[-1][-1]
if a==-1:
  print(-1)
else:
  print(H*W-black-a-1)

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy

H,W = map(int,input().split())
s = [list(input()) for _ in range(H)]
black=0

for tate in s:
  for yoko in tate:
    if yoko=="#":
      black += 1

def search(y,x,H,W,s,zumi):
  child=[]
      
  if y<H-1:
    if s[y+1][x]==".":
      if [y+1,x] not in zumi:
        child.append([y+1,x])
  if 0<y:
    if s[y-1][x]==".":
      if [y-1,x] not in zumi:
        child.append([y-1,x])
  if 0<x:
    if s[y][x-1]==".":
      if [y,x-1] not in zumi:
        child.append([y,x-1])
  if x<W-1:
    if s[y][x+1]==".":
      if [y,x+1] not in zumi:
        child.append([y,x+1])
  return child

def dfs(y,x,cnt,H,W,s,zumi):
  node_to_visit=search(y,x,H,W,s,zumi)
  zumi=deepcopy(node_to_visit)
  flag=True
  
  while flag:
    cnt += 1
    tmp=[]
    tmp2=[]
    #print(node_to_visit)
    zumi += node_to_visit
    for next_point in node_to_visit:
      t=next_point[0] #y。たて
      u=next_point[1] #x。よこ
      tmp=search(t,u,H,W,s,zumi)
      tmp2 += tmp
    node_to_visit=deepcopy(tmp2)
    if [H-1,W-1] in node_to_visit:
      white=cnt
      flag=False
      break     
  ans=H*W-white-black-2
  #print(white,black,ans)
  print(ans)

zumi=[]
dfs(0,0,0,H,W,s,zumi)

Fixed submission:
from collections import deque

H,W = map(int,input().split())
s = [list(input()) for _ in range(H)]
c = [[-1 for _ in range(W)] for _ in range(H)]

black=0
for tate in s:
  for yoko in tate:
    if yoko=="#":
      black += 1

def search(y,x,H,W,c,i):
  child=[]      
  if y<H-1:
    if s[y+1][x]=="." and c[y+1][x]==-1:
      c[y+1][x]=i
      child.append([y+1,x])
  if 0<y:
    if s[y-1][x]=="." and c[y-1][x]==-1:
      c[y-1][x]=i
      child.append([y-1,x])
  if 0<x:
    if s[y][x-1]=="." and c[y][x-1]==-1:
      c[y][x-1]=i
      child.append([y,x-1])
  if x<W-1:
    if s[y][x+1]=="." and c[y][x+1]==-1:
      c[y][x+1]=i
      child.append([y,x+1])
  return child

def dfs(y,x,c):
  visited=[[y,x]]
  c[y][x]=0
  i=1
  
  while visited:
    tmp=[]
    for a in visited:
      p=search(a[0],a[1],H,W,c,i)  #listが戻る
      tmp += p
    visited=tmp
    i += 1

dfs(0,0,c)
a=c[-1][-1]
if a==-1:
  print(-1)
else:
  print(H*W-black-a-1)

Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
s = [list(input()) for _ in range(H)]
black=0

for tate in s:
  for yoko in tate:
    if yoko=="#":
      black += 1

def search(y,x,H,W,s):
  child=[]
  if y<H-1:
    if s[y+1][x]==".":
      child.append([y+1,x])
  if 0<y:
    if s[y-1][x]==".":
      child.append([y-1,x])
  if 0<x:
    if s[y][x-1]==".":
      child.append([y,x-1])
  if x<W-1:
    if s[y][x+1]==".":
      child.append([y,x+1])
  return child

def dfs(y,x,cnt,H,W,s):
  node_to_visit=search(y,x,H,W,s)
  flag=True
  
  while flag:
    cnt += 1
    tmp=[]
    tmp2=[]
    for next_point in node_to_visit:
      t=next_point[0]
      u=next_point[1]
      if t!= y or u!=x:
        tmp=search(u,t,H,W,s)
      tmp2 += tmp
    node_to_visit=tmp2
    if [H-1,W-1] in node_to_visit:
      white=cnt
      flag=False
      break
      
  ans=H*W-white-black-2
  print(white,black,ans)

  
dfs(0,0,0,H,W,s)

Fixed submission:
from collections import deque

H,W = map(int,input().split())
s = [list(input()) for _ in range(H)]
c = [[-1 for _ in range(W)] for _ in range(H)]

black=0
for tate in s:
  for yoko in tate:
    if yoko=="#":
      black += 1

def search(y,x,H,W,c,i):
  child=[]      
  if y<H-1:
    if s[y+1][x]=="." and c[y+1][x]==-1:
      c[y+1][x]=i
      child.append([y+1,x])
  if 0<y:
    if s[y-1][x]=="." and c[y-1][x]==-1:
      c[y-1][x]=i
      child.append([y-1,x])
  if 0<x:
    if s[y][x-1]=="." and c[y][x-1]==-1:
      c[y][x-1]=i
      child.append([y,x-1])
  if x<W-1:
    if s[y][x+1]=="." and c[y][x+1]==-1:
      c[y][x+1]=i
      child.append([y,x+1])
  return child

def dfs(y,x,c):
  visited=[[y,x]]
  c[y][x]=0
  i=1
  
  while visited:
    tmp=[]
    for a in visited:
      p=search(a[0],a[1],H,W,c,i)  #listが戻る
      tmp += p
    visited=tmp
    i += 1

dfs(0,0,c)
a=c[-1][-1]
if a==-1:
  print(-1)
else:
  print(H*W-black-a-1)