Problem p03436 (python)

Fixed submission:
from collections import deque
def bfs():
  d=[[float("inf")]*m for _ in range(n)]
  dx=[1,0,-1,0]
  dy=[0,1,0,-1]
  
  que=deque([])
  que.append((0,0))
  d[0][0]=0

  while que:
    p = que.popleft()
    if p[0]==n-1 and p[1]==m-1:
      break
    for i in range(4):
      nx=p[0]+dx[i]
      ny=p[1]+dy[i]
      if 0 <= nx < n and 0 <= ny < m and c[nx][ny] != "#" and d[nx][ny] == float("inf"):
                # 移動できるならキューに入れ、その点の距離を p からの距離 +1 で確定する
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
  return d[n-1][m-1]

n, m = map(int, input().split())
c = [list(input()) for i in range(n)]

count=0
for i in range(n):
  for j in range(m):
    if c[i][j]=="#":
      count+=1
g=bfs()
if 0<g<float("inf"):
  print(n*m-g-1-count)
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
from collections import deque
h,w=map(int,input().split())
c=[list(input()) for _ in range(h)]
dhdw = [[-1,0],[1,0],[0,-1],[0,1]]
min_dis=[[-1]*w for _ in range(h)]
que=deque()
def bfs(x,y):
  que.append([x,y])
  min_dis[x][y]=0
  while que:
    sh,sw=que.popleft()
    for dh,dw in dhdw:
      nh,nw = sh+dh,sw+dw
      if not(0<=nh<=h-1) or not(0<=nw<=w-1) or min_dis[nh][nw]!=-1 or c[nh][nw]=='#':
        continue
      min_dis[nh][nw]=min_dis[sh][sw]+1
      que.append([nh,nw])
      if [nh,nw]==[h-1,w-1]:
        return min_dis[sh][sw]+1
a=bfs(0,0) or ans-2
ans=0
for i in c:
  ans+=i.count(".")
print(ans-1-a)

Buggy submission (verdict: Runtime Error):
from collections import deque
h,w=map(int,input().split())
c=[list(input()) for _ in range(h)]
dhdw = [[-1,0],[1,0],[0,-1],[0,1]]
min_dis=[[-1]*w for _ in range(h)]
que=deque()
def bfs(x,y):
  que.append([x,y])
  min_dis[x][y]=0
  while que:
    sh,sw=que.popleft()
    for dh,dw in dhdw:
      nh,nw = sh+dh,sw+dw
      if not(0<=nh<=h-1) or not(0<=nw<=w-1) or min_dis[nh][nw]!=-1 or c[nh][nw]=='#':
        continue
      min_dis[nh][nw]=min_dis[sh][sw]+1
      que.append([nh,nw])
      if [nh,nw]==[h-1,w-1]:
        return min_dis[sh][sw]+1
a=bfs(0,0)
ans=0
for i in c:
  ans+=i.count(".")
print(ans-1-a)

Buggy submission (verdict: Runtime Error):
10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Buggy submission (verdict: Runtime Error):
from collections import deque
def bfs():
  d=[[float("inf")]*m for _ in range(n)]
  dx=[1,0,-1,0]
  dy=[0,1,0,-1]
  
  que=deque([])
  que.append((0,0))
  d[0][0]=0

  while que:
    p = que.popleft()
    if p[0]==n and p[1]==m:
      break
    for i in range(4):
      nx=p[0]+dx[i]
      ny=p[1]+dy[i]
      if 0 <= nx < n and 0 <= ny < m and c[nx][ny] != "#" and d[nx][ny] == float("inf"):
                # 移動できるならキューに入れ、その点の距離を p からの距離 +1 で確定する
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
  return d[gx][gy]

n, m = map(int, input().split())
c = [list(input()) for i in range(n)]

count=0
for i in range(n):
  for j in range(m):
    if c[i][j]=="#":
      count+=1

print(n*m-bfs()-1-count)