Problem p03436 (python)

Fixed submission:
from collections import deque
H,W=map(int,input().split())
G=[input() for i in range(H)]

b_n= [G[i].count("#") for i in range(H)]

root_min=float("inf")
root=deque([[0,0,1]])
F=[[0]*W for i in range(H)]
while len(root)!=0:
  p=root.popleft()
  if p[:2]==[H-1,W-1]:
    print(H*W-p[2]-sum(b_n))
    exit()
  F[p[0]][p[1]]=1
  for i in range(4):
    if i==0:
      n=[max(0,p[0]-1),p[1],p[2]+1]
    elif i==1:
      n=[min(H-1,p[0]+1),p[1],p[2]+1]
    elif i==2:
      n=[p[0],max(0,p[1]-1),p[2]+1]
    else:
      n=[p[0],min(W-1,p[1]+1),p[2]+1]
      
    if G[n[0]][n[1]]=="#":
      continue
    if F[n[0]][n[1]]==1:
      continue
    if n in root:
      continue
    root.append(n)
print(-1)

Buggy submission (verdict: Runtime Error):
H,W=map(int,input().split())
G=[input() for i in range(H)]
F=[[0]*W for i in range(H)]

def judge(p0,p1,res):
  F[p0][p1]=1
  if p0==H-1 and p1==W-1:
    return res
  
  a=float("inf")
  if p0<H-1 and F[p0+1][p1]==0 and G[p0+1][p1]==".":
    m=judge(p0+1,p1,res+1)
    if m!=False:
      a=min(a,m)
  
  if p1<W-1 and F[p0][p1+1]==0 and G[p0][p1+1]==".":
    m=judge(p0,p1+1,res+1)
    if m!=False:
      a=min(a,m)
      
  if p0>0 and F[p0-1][p1]==0 and G[p0-1][p1]==".":
    m=judge(p0-1,p1,res+1)
    if m!=False:
      a=min(a,m)
   
  if p1>0 and F[p0][p1-1]==0 and G[p0][p1-1]==".":
    m=judge(p0,p1-1,res+1)
    if m!=False:
      a=min(a,m)
    
  if a!=float("inf"):
    return a
    
  return False
k=judge(0,0,0)
if k!=False:
  ans=W*H-(k+1)
  for i in range(H):
    ans-=G[i].count("#")
  print(ans)
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
H,W=map(int,input().split())
G=[input() for i in range(H)]
F=[[0]*W for i in range(H)]

def judge(p0,p1,res):
  if p0==H-1 and p1==W-1:
    return res
  
  a=float("inf")
  if p0<H-1 and F[p0+1][p1]==0 and G[p0+1][p1]==".":
    F[p0+1][p1]=1
    m=judge(p0+1,p1,res+1)
    if m!=False:
      a=min(a,m)
  
  if p1<W-1 and F[p0][p1+1]==0 and G[p0][p1+1]==".":
    F[p0][p1+1]=1
    m=judge(p0,p1+1,res+1)
    if m!=False:
      a=min(a,m)
      
  if p0>0 and F[p0-1][p1]==0 and G[p0-1][p1]==".":
    F[p0-1][p1]=1
    m=judge(p0-1,p1,res+1)
    if m!=False:
      a=min(a,m)
   
  if p1>0 and F[p0][p1-1]==0 and G[p0][p1-1]==".":
    F[p0][p1-1]=1
    m=judge(p0,p1-1,res+1)
    if m!=False:
      a=min(a,m)
    
  if a!=float("inf"):
    return a
    
  return False
k=judge(0,0,0)
if k!=False:
  ans=W*H-(k+1)
  for i in range(H):
    ans-=G[i].count("#")
  print(ans)
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
H,W=map(int,input().split())
G=[input() for i in range(H)]
F=[[0]*W for i in range(H)]

def judge(p0,p1,res):
  if p0==H-1 and p1==W-1:
    return res
  
  a=float("inf")
  if p0<H-1 and F[p0+1][p1]==0 and G[p0+1][p1]==".":
    F[p0+1][p1]=1
    m=judge(p0+1,p1,res+1)
    if m!=False:
      a=min(a,m)
  
  if p1<W-1 and F[p0][p1+1]==0 and G[p0][p1+1]==".":
    F[p0][p1+1]=1
    m=judge(p0,p1+1,res+1)
    if m!=False:
      a=min(a,m)
      
  if p0>0 and F[p0-1][p1]==0 and G[p0-1][p1]==".":
    F[p0-1][p1]=1
    m=judge(p0-1,p1,res+1)
    if m!=False:
      a=min(a,m)
   
  if p1>0 and F[p0][p1-1]==0 and G[p0][p1-1]==".":
    F[p0][p1-1]=1
    m=judge(p0,p1-1,res+1)
    if m!=False:
      a=min(a,m)
    
  if a!=float("inf"):
    return a
    
  return False
k=judge(0,0,0)
if k:
  ans=W*H-(k+1)
  for i in range(H):
    ans-=G[i].count("#")
  print(ans)
else:
  print(-1)