Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
MAX_VAL = 10 ** 9
H, W = map(int, input().split())



border_row = "#"*(W+2)
field = [border_row]
num_blacks = 0
for i in range(H):
    line = input()
    num_blacks += line.count(".")
    field.append("#"+line+"#")
field.append(border_row)

cost = [[MAX_VAL]*(W+2) for _ in range(H+2)]

if field[1][1] == ".":
    cost[1][1] = 1
else:
    cost[1][1] = MAX_VAL

visited = [[False]*(W+2) for _ in range(H+2)] 
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((1, 1))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if field[ny][nx] == ".":
                cost[ny][nx] = min(cost[ny][nx], cost[y][x]+1)
                if not visited[ny][nx]:
                    q.append((nx, ny))
        visited[y][x] = True

bfs()
print(num_blacks - cost[H][W]) if cost[H][W] < MAX_VAL else print(-1)




Fixed submission:
from collections import deque
H, W = map(int, input().split())


field = []
num_blacks = 0
for i in range(H):
    line = input()
    num_blacks += line.count(".")
    field.append(line)

cost = [[-1]*(W) for _ in range(H)]

cost[0][0] = 1
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((0, 0))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= W or ny < 0 or ny >= H:
                continue

            if field[ny][nx] == "." and cost[ny][nx] < 0:
                cost[ny][nx] = cost[y][x]+1
                q.append((nx, ny))

bfs()
print(num_blacks - cost[H-1][W-1]) if cost[H-1][W-1] > 0 else print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
MAX_VAL = 10 ** 9
H, W = map(int, input().split())



border_row = "#"*(W+2)
field = [border_row]
for i in range(H):
    line = input()
    field.append("#"+line+"#")
field.append(border_row)

cost = [[MAX_VAL]*(W+2) for _ in range(H+2)]

if field[1][1] == ".":
    cost[1][1] = 1
else:
    cost[1][1] = MAX_VAL

visited = [[False]*(W+2) for _ in range(H+2)] 
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((1, 1))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if field[ny][nx] == ".":
                cost[ny][nx] = min(cost[ny][nx], cost[y][x]+1)
                if not visited[ny][nx]:
                    q.append((nx, ny))
        visited[y][x] = True

bfs()
num_blacks = 0
for line in field[1:H+1]:
    for c in line[1:W+1]:
        if c == "#":
            num_blacks += 1
print(H*W-num_blacks-cost[H][W]) if cost[H][W] < MAX_VAL else print(-1)




Fixed submission:
from collections import deque
H, W = map(int, input().split())


field = []
num_blacks = 0
for i in range(H):
    line = input()
    num_blacks += line.count(".")
    field.append(line)

cost = [[-1]*(W) for _ in range(H)]

cost[0][0] = 1
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((0, 0))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= W or ny < 0 or ny >= H:
                continue

            if field[ny][nx] == "." and cost[ny][nx] < 0:
                cost[ny][nx] = cost[y][x]+1
                q.append((nx, ny))

bfs()
print(num_blacks - cost[H-1][W-1]) if cost[H-1][W-1] > 0 else print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
MAX_VAL = 10 ** 9
H, W = map(int, input().split())



border_row = "#"*(W+2)
field = [border_row]
for i in range(H):
    line = input()
    field.append("#"+line+"#")
field.append(border_row)

cost = [[MAX_VAL]*(W+2) for _ in range(H+2)]

if field[1][1] == ".":
    cost[1][1] = 1
else:
    cost[1][1] = MAX_VAL

visited = [[False]*(W+2) for _ in range(H+2)] 
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((1, 1))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if field[ny][nx] == ".":
                cost[ny][nx] = min(cost[ny][nx], cost[y][x]+1)
                if not visited[ny][nx]:
                    q.append((nx, ny))
        visited[y][x] = True

bfs()
num_blacks = 0
for line in field[1:H+1]:
    for c in line[1:W+1]:
        if c == "#":
            num_blacks += 1
print(H*W-num_blacks-cost[H][W]) if cost[H][W] < MAX_VAL else print(-1)




Fixed submission:
from collections import deque
H, W = map(int, input().split())


field = []
num_blacks = 0
for i in range(H):
    line = input()
    num_blacks += line.count(".")
    field.append(line)

cost = [[-1]*(W) for _ in range(H)]

cost[0][0] = 1
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((0, 0))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= W or ny < 0 or ny >= H:
                continue

            if field[ny][nx] == "." and cost[ny][nx] < 0:
                cost[ny][nx] = cost[y][x]+1
                q.append((nx, ny))

bfs()
print(num_blacks - cost[H-1][W-1]) if cost[H-1][W-1] > 0 else print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
MAX_VAL = 10 ** 9
H, W = map(int, input().split())



border_row = "#"*(W+2)
field = [border_row]
num_blacks = 0
for i in range(H):
    line = input()
    for c in line:
        if c == "#":
            num_blacks += 1
    field.append("#"+line+"#")
field.append(border_row)

cost = [[MAX_VAL]*(W+2) for _ in range(H+2)]

if field[1][1] == ".":
    cost[1][1] = 1
else:
    cost[1][1] = MAX_VAL

visited = [[False]*(W+2) for _ in range(H+2)] 
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((1, 1))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if field[ny][nx] == ".":
                cost[ny][nx] = min(cost[ny][nx], cost[y][x]+1)
                if not visited[ny][nx]:
                    q.append((nx, ny))
        visited[y][x] = True

bfs()
print(H*W-num_blacks-cost[H][W]) if cost[H][W] < MAX_VAL else print(-1)




Fixed submission:
from collections import deque
H, W = map(int, input().split())


field = []
num_blacks = 0
for i in range(H):
    line = input()
    num_blacks += line.count(".")
    field.append(line)

cost = [[-1]*(W) for _ in range(H)]

cost[0][0] = 1
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((0, 0))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= W or ny < 0 or ny >= H:
                continue

            if field[ny][nx] == "." and cost[ny][nx] < 0:
                cost[ny][nx] = cost[y][x]+1
                q.append((nx, ny))

bfs()
print(num_blacks - cost[H-1][W-1]) if cost[H-1][W-1] > 0 else print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
MAX_VAL = 10 ** 9
H, W = map(int, input().split())



border_row = "#"*(W+2)
field = [border_row]
num_blacks = 0
for i in range(H):
    line = input()
    for c in line:
        if c == "#":
            num_blacks += 1
    field.append("#"+line+"#")
field.append(border_row)

cost = [[MAX_VAL]*(W+2) for _ in range(H+2)]

if field[1][1] == ".":
    cost[1][1] = 1
else:
    cost[1][1] = MAX_VAL

visited = [[False]*(W+2) for _ in range(H+2)] 
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((1, 1))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if field[ny][nx] == ".":
                cost[ny][nx] = min(cost[ny][nx], cost[y][x]+1)
                if not visited[ny][nx]:
                    q.append((nx, ny))
        visited[y][x] = True

bfs()
print(H*W-num_blacks-cost[H][W]) if cost[H][W] < MAX_VAL else print(-1)




Fixed submission:
from collections import deque
H, W = map(int, input().split())


field = []
num_blacks = 0
for i in range(H):
    line = input()
    num_blacks += line.count(".")
    field.append(line)

cost = [[-1]*(W) for _ in range(H)]

cost[0][0] = 1
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((0, 0))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= W or ny < 0 or ny >= H:
                continue

            if field[ny][nx] == "." and cost[ny][nx] < 0:
                cost[ny][nx] = cost[y][x]+1
                q.append((nx, ny))

bfs()
print(num_blacks - cost[H-1][W-1]) if cost[H-1][W-1] > 0 else print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, Counter
from pprint import pprint
MAX_VAL = 10 ** 9
H, W = map(int, input().split())



border_row = "#"*(W+2)
field = [border_row]
c = Counter()
for i in range(H):
    line = input()
    temp_c = Counter(line)
    c += temp_c
    field.append("#"+line+"#")
field.append(border_row)

cost = [[MAX_VAL]*(W+2) for _ in range(H+2)]

if field[1][1] == ".":
    cost[1][1] = 1
else:
    cost[1][1] = MAX_VAL

visited = [[False]*(W+2) for _ in range(H+2)] 
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((1, 1))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if field[ny][nx] == ".":
                cost[ny][nx] = min(cost[ny][nx], cost[y][x]+1)
                if not visited[ny][nx]:
                    q.append((nx, ny))
        visited[y][x] = True

bfs()
# pprint(cost)
# print(cost[H][W])
print(H*W-c["#"]-cost[H][W]) if cost[H][W] < MAX_VAL else print(-1)




Fixed submission:
from collections import deque
H, W = map(int, input().split())


field = []
num_blacks = 0
for i in range(H):
    line = input()
    num_blacks += line.count(".")
    field.append(line)

cost = [[-1]*(W) for _ in range(H)]

cost[0][0] = 1
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def bfs():
    q = deque()
    q.append((0, 0))
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= W or ny < 0 or ny >= H:
                continue

            if field[ny][nx] == "." and cost[ny][nx] < 0:
                cost[ny][nx] = cost[y][x]+1
                q.append((nx, ny))

bfs()
print(num_blacks - cost[H-1][W-1]) if cost[H-1][W-1] > 0 else print(-1)