Buggy python submission (verdict: Time Limit Exceeded):
H, W = list(map(int, input().split()))
grid = []
for _ in range(H):
    grid.append(list(input()))
dist_grid = [[-1 for _ in range(W)] for __ in range(H)]

queue = [(0, 0, 1)]
while queue:
    h, w, cnt = queue.pop(0)
    dist_grid[h][w] = cnt
    nexts = [(h+1, w, cnt+1), (h-1, w, cnt+1), (h, w+1, cnt+1), (h, w-1, cnt+1)]
    for y, x, c in nexts:
        if y < 0 or H <= y or x < 0 or W <= x:
            continue
        if dist_grid[y][x] == -1 and grid[y][x] == '.':
            queue.append((y, x, c))
            
sharp_num = 0
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            sharp_num += 1

if dist_grid[-1][-1] == -1:
    print(-1)
else:
    print(H * W - dist_grid[-1][-1] - sharp_num)





Fixed submission:
import sys
from collections import deque
 
h,w = map(int, sys.stdin.readline().split())
maze = [list(sys.stdin.readline()) for _ in range(h)]
dis = [[-1 for _ in range(w)] for _ in range(h)]
 
que = deque()
que.append((0,0))
dis[0][0] = 1
 
while que:
    y,x = que.popleft()
    for dx, dy in ((1,0), (0,1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if (nx < 0) or (nx > w-1) or (ny < 0) or (ny > h-1): continue
        if (maze[ny][nx] == '.') and (dis[ny][nx] == -1):
            dis[ny][nx] = dis[y][x] + 1
            que.append((ny, nx))
 
ans = 0
black_num = 0
for m in maze:
    black_num += m.count('#')
 
if dis[h-1][w-1] == -1:
    ans = -1
else:
    ans = w*h - black_num - dis[h-1][w-1]
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = list(map(int, input().split()))
grid = []
for _ in range(H):
    grid.append(list(input()))
dist_grid = [[-1 for _ in range(W)] for __ in range(H)]

queue = [(0, 0, 1)]
while queue:
    h, w, cnt = queue.pop(0)
    dist_grid[h][w] = cnt
    nexts = [(h+1, w, cnt+1), (h-1, w, cnt+1), (h, w+1, cnt+1), (h, w-1, cnt+1)]
    for y, x, c in nexts:
        if 0 <= y and y < H and 0 <= x and x < W:
            if dist_grid[y][x] == -1 and grid[y][x] != '#':
                queue.append((y, x, c))
            
sharp_num = 0
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            sharp_num += 1

if dist_grid[-1][-1] == -1:
    print(-1)
else:
    print(H * W - dist_grid[-1][-1] - sharp_num)





Fixed submission:
import sys
from collections import deque
 
h,w = map(int, sys.stdin.readline().split())
maze = [list(sys.stdin.readline()) for _ in range(h)]
dis = [[-1 for _ in range(w)] for _ in range(h)]
 
que = deque()
que.append((0,0))
dis[0][0] = 1
 
while que:
    y,x = que.popleft()
    for dx, dy in ((1,0), (0,1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if (nx < 0) or (nx > w-1) or (ny < 0) or (ny > h-1): continue
        if (maze[ny][nx] == '.') and (dis[ny][nx] == -1):
            dis[ny][nx] = dis[y][x] + 1
            que.append((ny, nx))
 
ans = 0
black_num = 0
for m in maze:
    black_num += m.count('#')
 
if dis[h-1][w-1] == -1:
    ans = -1
else:
    ans = w*h - black_num - dis[h-1][w-1]
 
print(ans)

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

H, W = map(int, sys.stdin.readline().strip().split())
grid = [list(sys.stdin.readline().strip()) for _ in range(H)]
dq = deque([(0, 0, 1)])
nexts = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while dq:
    h, w, cnt = dq.popleft()
    grid[h][w] = cnt
    for y, x in nexts:
        if 0 <= y + h and y + h < H and 0 <= x + w and x + w < W and grid[y + h][x + w] == '.':
            dq.append((y + h, x + w, cnt + 1))
            
sharp_num = 0
max_num = 0
for h in range(H):
    sharp_num += grid[h].count('#')
if grid[-1][-1] == '.' or grid[-1][-1] == '#':
    print(-1)
else:
    print(H * W - grid[-1][-1] - sharp_num)





Fixed submission:
import sys
from collections import deque
 
h,w = map(int, sys.stdin.readline().split())
maze = [list(sys.stdin.readline()) for _ in range(h)]
dis = [[-1 for _ in range(w)] for _ in range(h)]
 
que = deque()
que.append((0,0))
dis[0][0] = 1
 
while que:
    y,x = que.popleft()
    for dx, dy in ((1,0), (0,1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if (nx < 0) or (nx > w-1) or (ny < 0) or (ny > h-1): continue
        if (maze[ny][nx] == '.') and (dis[ny][nx] == -1):
            dis[ny][nx] = dis[y][x] + 1
            que.append((ny, nx))
 
ans = 0
black_num = 0
for m in maze:
    black_num += m.count('#')
 
if dis[h-1][w-1] == -1:
    ans = -1
else:
    ans = w*h - black_num - dis[h-1][w-1]
 
print(ans)

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

H, W = map(int, sys.stdin.readline().strip().split())
grid = [list(sys.stdin.readline().strip()) for _ in range(H)]
dq = deque([(0, 0, 1)])
while dq:
    h, w, cnt = dq.popleft()
    grid[h][w] = cnt
    nexts = [(h+1, w, cnt+1), (h-1, w, cnt+1), (h, w+1, cnt+1), (h, w-1, cnt+1)]
    for y, x, c in nexts:
        if 0 <= y and y < H and 0 <= x and x < W and grid[y][x] == '.':
            dq.append((y, x, c))
            
sharp_num = 0
max_num = 0
for h in range(H):
    sharp_num += grid[h].count('#')
if grid[-1][-1] == '.' or grid[-1][-1] == '#':
    print(-1)
else:
    print(H * W - grid[-1][-1] - sharp_num)





Fixed submission:
import sys
from collections import deque
 
h,w = map(int, sys.stdin.readline().split())
maze = [list(sys.stdin.readline()) for _ in range(h)]
dis = [[-1 for _ in range(w)] for _ in range(h)]
 
que = deque()
que.append((0,0))
dis[0][0] = 1
 
while que:
    y,x = que.popleft()
    for dx, dy in ((1,0), (0,1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if (nx < 0) or (nx > w-1) or (ny < 0) or (ny > h-1): continue
        if (maze[ny][nx] == '.') and (dis[ny][nx] == -1):
            dis[ny][nx] = dis[y][x] + 1
            que.append((ny, nx))
 
ans = 0
black_num = 0
for m in maze:
    black_num += m.count('#')
 
if dis[h-1][w-1] == -1:
    ans = -1
else:
    ans = w*h - black_num - dis[h-1][w-1]
 
print(ans)

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

H, W = map(int, sys.stdin.readline().strip().split())
grid = [list(sys.stdin.readline().strip()) for _ in range(H)]
dq = deque([(0, 0, 1)])
while dq:
    h, w, cnt = dq.popleft()
    grid[h][w] = cnt
    nexts = [(h+1, w, cnt+1), (h-1, w, cnt+1), (h, w+1, cnt+1), (h, w-1, cnt+1)]
    for y, x, c in nexts:
        if 0 <= y and y < H and 0 <= x and x < W:
            if grid[y][x] == '.':
                dq.append((y, x, c))
            if type(grid[y][x]) == int and c < grid[y][x]:
                dq.append((y, x, c))

sharp_num = 0
max_num = 0
for h in range(H):
    sharp_num += grid[h].count('#')
if grid[-1][-1] == '.' or grid[-1][-1] == '#':
    print(-1)
else:
    print(H * W - grid[-1][-1] - sharp_num)





Fixed submission:
import sys
from collections import deque
 
h,w = map(int, sys.stdin.readline().split())
maze = [list(sys.stdin.readline()) for _ in range(h)]
dis = [[-1 for _ in range(w)] for _ in range(h)]
 
que = deque()
que.append((0,0))
dis[0][0] = 1
 
while que:
    y,x = que.popleft()
    for dx, dy in ((1,0), (0,1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if (nx < 0) or (nx > w-1) or (ny < 0) or (ny > h-1): continue
        if (maze[ny][nx] == '.') and (dis[ny][nx] == -1):
            dis[ny][nx] = dis[y][x] + 1
            que.append((ny, nx))
 
ans = 0
black_num = 0
for m in maze:
    black_num += m.count('#')
 
if dis[h-1][w-1] == -1:
    ans = -1
else:
    ans = w*h - black_num - dis[h-1][w-1]
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W = list(map(int, input().split()))
grid = []
for _ in range(H):
    grid.append(list(input()))

dq = deque([(0, 0, 1)])
while dq:
    h, w, cnt = dq.popleft()
    grid[h][w] = cnt
    nexts = [(h+1, w, cnt+1), (h-1, w, cnt+1), (h, w+1, cnt+1), (h, w-1, cnt+1)]
    for y, x, c in nexts:
        if 0 <= y and y < H and 0 <= x and x < W:
            if grid[y][x] == '.':
                dq.append((y, x, c))
            if type(grid[y][x]) == int and c < grid[y][x]:
                dq.append((y, x, c))

if grid[-1][-1] == '.' or grid[-1][-1] == '#':
    print(-1)
    exit() 

sharp_num = 0
max_num = 0
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            sharp_num += 1
print(H * W - grid[-1][-1] - sharp_num)





Fixed submission:
import sys
from collections import deque
 
h,w = map(int, sys.stdin.readline().split())
maze = [list(sys.stdin.readline()) for _ in range(h)]
dis = [[-1 for _ in range(w)] for _ in range(h)]
 
que = deque()
que.append((0,0))
dis[0][0] = 1
 
while que:
    y,x = que.popleft()
    for dx, dy in ((1,0), (0,1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if (nx < 0) or (nx > w-1) or (ny < 0) or (ny > h-1): continue
        if (maze[ny][nx] == '.') and (dis[ny][nx] == -1):
            dis[ny][nx] = dis[y][x] + 1
            que.append((ny, nx))
 
ans = 0
black_num = 0
for m in maze:
    black_num += m.count('#')
 
if dis[h-1][w-1] == -1:
    ans = -1
else:
    ans = w*h - black_num - dis[h-1][w-1]
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = list(map(int, input().split()))
grid = []
for _ in range(H):
    grid.append(list(input()))

queue = [(0, 0, 1)]
while queue:
    h, w, cnt = queue.pop(0)
    grid[h][w] = cnt
    nexts = [(h+1, w, cnt+1), (h-1, w, cnt+1), (h, w+1, cnt+1), (h, w-1, cnt+1)]
    for y, x, c in nexts:
        if 0 <= y and y < H and 0 <= x and x < W:
            if grid[y][x] == '#':
                continue
            elif grid[y][x] == '.':
                queue.append((y, x, c))
            elif c < grid[y][x]:
                queue.append((y, x, c))

if grid[-1][-1] == '.' or grid[-1][-1] == '#':
    print(-1)
    exit() 

sharp_num = 0
max_num = 0
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            sharp_num += 1
print(H * W - grid[-1][-1] - sharp_num)





Fixed submission:
import sys
from collections import deque
 
h,w = map(int, sys.stdin.readline().split())
maze = [list(sys.stdin.readline()) for _ in range(h)]
dis = [[-1 for _ in range(w)] for _ in range(h)]
 
que = deque()
que.append((0,0))
dis[0][0] = 1
 
while que:
    y,x = que.popleft()
    for dx, dy in ((1,0), (0,1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if (nx < 0) or (nx > w-1) or (ny < 0) or (ny > h-1): continue
        if (maze[ny][nx] == '.') and (dis[ny][nx] == -1):
            dis[ny][nx] = dis[y][x] + 1
            que.append((ny, nx))
 
ans = 0
black_num = 0
for m in maze:
    black_num += m.count('#')
 
if dis[h-1][w-1] == -1:
    ans = -1
else:
    ans = w*h - black_num - dis[h-1][w-1]
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = list(map(int, input().split()))
grid = []
for _ in range(H):
    grid.append(list(input()))

queue = [(0, 0, 1)]
while queue:
    h, w, cnt = queue.pop(0)
    grid[h][w] = cnt
    nexts = [(h+1, w, cnt+1), (h-1, w, cnt+1), (h, w+1, cnt+1), (h, w-1, cnt+1)]
    for y, x, c in nexts:
        if 0 <= y and y < H and 0 <= x and x < W:
            if grid[y][x] == '#':
                continue
            elif grid[y][x] == '.':
                queue.append((y, x, c))
            elif c < grid[y][x]:
                queue.append((y, x, c))

if grid[-1][-1] == '.' or grid[-1][-1] == '#':
    print(-1)
    exit() 

sharp_num = 0
max_num = 0
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            sharp_num += 1
print(H * W - grid[-1][-1] - sharp_num)





Fixed submission:
import sys
from collections import deque
 
h,w = map(int, sys.stdin.readline().split())
maze = [list(sys.stdin.readline()) for _ in range(h)]
dis = [[-1 for _ in range(w)] for _ in range(h)]
 
que = deque()
que.append((0,0))
dis[0][0] = 1
 
while que:
    y,x = que.popleft()
    for dx, dy in ((1,0), (0,1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if (nx < 0) or (nx > w-1) or (ny < 0) or (ny > h-1): continue
        if (maze[ny][nx] == '.') and (dis[ny][nx] == -1):
            dis[ny][nx] = dis[y][x] + 1
            que.append((ny, nx))
 
ans = 0
black_num = 0
for m in maze:
    black_num += m.count('#')
 
if dis[h-1][w-1] == -1:
    ans = -1
else:
    ans = w*h - black_num - dis[h-1][w-1]
 
print(ans)