Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
s = [list(input()) for x in range(H)]
d = [[None for y in range(W)] for x in range(H)]
q = []
white = 0
for x in range(H):
  for y in range(W):
    if s[x][y] == '.':
      white += 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
q.append([0, 0, 0])

def Haba(x_now, y_now, d_now, s, d, q):
  s[x_now][y_now] = '#'
  d[x_now][y_now] = d_now
  for i in range(4):
    if 0 <= x_now + dx[i] and x_now + dx[i] < H and 0 <= y_now + dy[i] and y_now + dy[i] < W:
      if s[x_now + dx[i]][y_now + dy[i]] == '.':
        q.append([x_now+dx[i], y_now+dy[i], d_now + 1])
while(len(q) != 0):
  x_now, y_now, d_now = q.pop(0)
  Haba(x_now, y_now, d_now, s, d, q)
if d[H - 1][W - 1] is not None:
  ans = white - d[H - 1][W - 1]  - 1
else:
  ans = -1
print(ans)

Fixed submission:
H, W = map(int, input().split())
s = [list(input()) for x in range(H)]
d = [[None for y in range(W)] for x in range(H)]
q = []
white = 0
for x in range(H):
  for y in range(W):
    if s[x][y] == '.':
      white += 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
q.append([0, 0, 0])

def Haba(x_now, y_now, d_now, s, d, q):
  for i in range(4):
    if 0 <= x_now + dx[i] and x_now + dx[i] < H and 0 <= y_now + dy[i] and y_now + dy[i] < W:
      if s[x_now + dx[i]][y_now + dy[i]] == '.':
        s[x_now + dx[i]][y_now + dy[i]] = '#'
        d[x_now + dx[i]][y_now + dy[i]] = d_now + 1
        q.append([x_now+dx[i], y_now+dy[i], d_now + 1])

while(len(q) > 0):
  x_now, y_now, d_now = q.pop(0)
  Haba(x_now, y_now, d_now, s, d, q)
if d[H - 1][W - 1] is not None:
  ans = white - d[H - 1][W - 1]  - 1
else:
  ans = -1
print(ans)

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

def Haba(q, x, y, move):
        s[x][y]= "#"
        if x == H - 1 and y == W - 1:
            return True
        for i in range(4):
            if 0 <= x + dx[i] and x + dx[i] < H and 0 <= y + dy[i] and y + dy[i] < W and s[x+dx[i]][y+dy[i]] == '.':
                move += 1
                q.append([x + dx[i], y + dy[i], move])
        return False

count = 0
for x in range(H):
    for y in range(W):
        if s[x][y] == '.':
            count += 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
q = []
q.append([0, 0, 0])
x = 0
y = 0
move = 0
ans = True
while(True):
    if len(q) == 0:
        ans = False
        break
    x, y, move = q.pop(0)
    if(Haba(q, x, y, move)) :
        break
if ans:
    print(count -move - 1)
else
    print(-1)

Fixed submission:
H, W = map(int, input().split())
s = [list(input()) for x in range(H)]
d = [[None for y in range(W)] for x in range(H)]
q = []
white = 0
for x in range(H):
  for y in range(W):
    if s[x][y] == '.':
      white += 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
q.append([0, 0, 0])

def Haba(x_now, y_now, d_now, s, d, q):
  for i in range(4):
    if 0 <= x_now + dx[i] and x_now + dx[i] < H and 0 <= y_now + dy[i] and y_now + dy[i] < W:
      if s[x_now + dx[i]][y_now + dy[i]] == '.':
        s[x_now + dx[i]][y_now + dy[i]] = '#'
        d[x_now + dx[i]][y_now + dy[i]] = d_now + 1
        q.append([x_now+dx[i], y_now+dy[i], d_now + 1])

while(len(q) > 0):
  x_now, y_now, d_now = q.pop(0)
  Haba(x_now, y_now, d_now, s, d, q)
if d[H - 1][W - 1] is not None:
  ans = white - d[H - 1][W - 1]  - 1
else:
  ans = -1
print(ans)