from collections import deque



def bfs():

    d = [[float("inf")] * w for i in range(h)]



    dx = [1, 0, -1, 0]

    dy = [0, 1, 0, -1]



    que = deque([])

    que.append((sx, sy))

    d[sx][sy] = 0



    while que:

        p = que.popleft()

        if p[0] == gx and p[1] == gy:

            break



        for i in range(4):

            nx = p[0] + dx[i]

            ny = p[1] + dy[i]



            if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and d[nx][ny] == float("inf"):

                que.append((nx, ny))

                d[nx][ny] = d[p[0]][p[1]] + 1

    

    return d[gx][gy]



h, w = list(map(int, input().split()))

maze = [list(eval(input())) for _ in range(h)]

sx, sy = 0, 0

gx, gy = h-1, w-1



white = 0

for i in range(h):

    for j in range(w):

        if maze[i][j] == ".":

            white += 1



res = bfs()



if 0 < res < float("inf"):

    print((white-res-1))

else:

    print((-1))
from collections import deque



H, W = list(map(int, input().split()))

s = [list(eval(input())) for _ in range(H)]



block_count = 0

for i in range(H):

    for j in range(W):

        if s[i][j] == '#':

            block_count += 1



d = [[0] * W for _ in range(H)]



dxdy = [(1, 0), (-1, 0), (0, 1), (0, -1)]



que = deque([])

que.append((0, 0))

d[0][0] = 1

while que:

    x, y = que.popleft()

    

    for dx, dy in dxdy:

        nx = x + dx

        ny = y + dy

        if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == 0 and s[ny][nx] == '.':

            que.append((nx, ny))

            d[ny][nx] = d[y][x] + 1



if d[H-1][W-1]:

    print((H * W - d[H-1][W-1] - block_count))

else:

    print((-1))

from collections import deque


def bfs():
    d = [[float("inf")] * w for i in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0
    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (
                0 <= nx < h
                and 0 <= ny < w
                and maze[nx][ny] != "#"
                and d[nx][ny] == float("inf")
            ):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
    return d[gx][gy]


h, w = list(map(int, input().split()))
maze = [list(eval(input())) for _ in range(h)]
sx, sy = 0, 0
gx, gy = h - 1, w - 1
white = 0
for i in range(h):
    for j in range(w):
        if maze[i][j] == ".":
            white += 1
res = bfs()
if 0 < res < float("inf"):
    print((white - res - 1))
else:
    print((-1))

from collections import deque

H, W = list(map(int, input().split()))
s = [list(eval(input())) for _ in range(H)]
block_count = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            block_count += 1
d = [[0] * W for _ in range(H)]
dxdy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
que = deque([])
que.append((0, 0))
d[0][0] = 1
while que:
    x, y = que.popleft()
    for dx, dy in dxdy:
        nx = x + dx
        ny = y + dy
        if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == 0 and s[ny][nx] == ".":
            que.append((nx, ny))
            d[ny][nx] = d[y][x] + 1
if d[H - 1][W - 1]:
    print((H * W - d[H - 1][W - 1] - block_count))
else:
    print((-1))

- -def bfs(): -    d = [[float("inf")] * w for i in range(h)] -    dx = [1, 0, -1, 0] -    dy = [0, 1, 0, -1] -    que = deque([]) -    que.append((sx, sy)) -    d[sx][sy] = 0 -    while que: -        p = que.popleft() -        if p[0] == gx and p[1] == gy: -            break -        for i in range(4): -            nx = p[0] + dx[i] -            ny = p[1] + dy[i] -            if ( -                0 <= nx < h -                and 0 <= ny < w -                and maze[nx][ny] != "#" -                and d[nx][ny] == float("inf") -            ): -                que.append((nx, ny)) -                d[nx][ny] = d[p[0]][p[1]] + 1 -    return d[gx][gy] - - -h, w = list(map(int, input().split())) -maze = [list(eval(input())) for _ in range(h)] -sx, sy = 0, 0 -gx, gy = h - 1, w - 1 -white = 0 -for i in range(h): -    for j in range(w): -        if maze[i][j] == ".": -            white += 1 -res = bfs() -if 0 < res < float("inf"): -    print((white - res - 1)) +H, W = list(map(int, input().split())) +s = [list(eval(input())) for _ in range(H)] +block_count = 0 +for i in range(H): +    for j in range(W): +        if s[i][j] == "#": +            block_count += 1 +d = [[0] * W for _ in range(H)] +dxdy = [(1, 0), (-1, 0), (0, 1), (0, -1)] +que = deque([]) +que.append((0, 0)) +d[0][0] = 1 +while que: +    x, y = que.popleft() +    for dx, dy in dxdy: +        nx = x + dx +        ny = y + dy +        if 0 <= nx < W and 0 <= ny < H and d[ny][nx] == 0 and s[ny][nx] == ".": +            que.append((nx, ny)) +            d[ny][nx] = d[y][x] + 1 +if d[H - 1][W - 1]: +    print((H * W - d[H - 1][W - 1] - block_count))