

from collections import deque

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

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

moves = ((1, 0), (0, 1), (0, -1), (-1, 0))



def bfs(sy, sx):

    q = deque([[sy, sx]])

    dist = [[float('inf')]*w for _ in range(h)]

    dist[sy][sx] = 0 



    while q:

        y, x = q.popleft()

        for dy, dx in moves:

            ny = y + dy 

            nx = x + dx 

            if 0 <= ny < h and 0 <= nx < w and dist[ny][nx] == float('inf') and s[ny][nx] != '#':

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

                q += [[ny, nx]]



    return dist 



d = bfs(0, 0)[h-1][w-1]

if d == float('inf'):

    print((-1))

    exit()

block = 0

for i in range(h):

    for j in range(w):

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

            block += 1

print((h*w-d-block-1))


from collections import deque

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

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

moves = ((1, 0), (0, 1), (0, -1), (-1, 0))



def bfs(sy, sx):

    q = deque([[sy, sx]])

    dist = [[float('inf')]*(W) for _ in range(H)]

    dist[sy][sx] = 0



    while q:

        y, x = q.popleft()

        for dy, dx in moves:

            ny = y + dy

            nx = x + dx

            if 0 <= ny < H and 0 <= nx < W and s[ny][nx] == '.' and dist[ny][nx] == float('inf'):

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

                q += [[ny, nx]]



    return dist



d = bfs(0, 0)



if d[-1][-1] == float('inf'):

    print((-1))

    exit()



ans = H*W 

for i in range(H):

    for j in range(W):

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

            ans -= 1



ans -= d[-1][-1] + 1

print(ans)
from collections import deque

h, w = list(map(int, input().split()))
s = [list(eval(input())) for _ in range(h)]
moves = ((1, 0), (0, 1), (0, -1), (-1, 0))


def bfs(sy, sx):
    q = deque([[sy, sx]])
    dist = [[float("inf")] * w for _ in range(h)]
    dist[sy][sx] = 0
    while q:
        y, x = q.popleft()
        for dy, dx in moves:
            ny = y + dy
            nx = x + dx
            if (
                0 <= ny < h
                and 0 <= nx < w
                and dist[ny][nx] == float("inf")
                and s[ny][nx] != "#"
            ):
                dist[ny][nx] = dist[y][x] + 1
                q += [[ny, nx]]
    return dist


d = bfs(0, 0)[h - 1][w - 1]
if d == float("inf"):
    print((-1))
    exit()
block = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            block += 1
print((h * w - d - block - 1))

from collections import deque

H, W = list(map(int, input().split()))
s = [list(eval(input())) for _ in range(H)]
moves = ((1, 0), (0, 1), (0, -1), (-1, 0))


def bfs(sy, sx):
    q = deque([[sy, sx]])
    dist = [[float("inf")] * (W) for _ in range(H)]
    dist[sy][sx] = 0
    while q:
        y, x = q.popleft()
        for dy, dx in moves:
            ny = y + dy
            nx = x + dx
            if (
                0 <= ny < H
                and 0 <= nx < W
                and s[ny][nx] == "."
                and dist[ny][nx] == float("inf")
            ):
                dist[ny][nx] = dist[y][x] + 1
                q += [[ny, nx]]
    return dist


d = bfs(0, 0)
if d[-1][-1] == float("inf"):
    print((-1))
    exit()
ans = H * W
for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            ans -= 1
ans -= d[-1][-1] + 1
print(ans)

-h, w = list(map(int, input().split())) -s = [list(eval(input())) for _ in range(h)] +H, W = list(map(int, input().split())) +s = [list(eval(input())) for _ in range(H)] -    dist = [[float("inf")] * w for _ in range(h)] +    dist = [[float("inf")] * (W) for _ in range(H)] -                0 <= ny < h -                and 0 <= nx < w +                0 <= ny < H +                and 0 <= nx < W +                and s[ny][nx] == "." -                and s[ny][nx] != "#" -d = bfs(0, 0)[h - 1][w - 1] -if d == float("inf"): +d = bfs(0, 0) +if d[-1][-1] == float("inf"): -block = 0 -for i in range(h): -    for j in range(w): +ans = H * W +for i in range(H): +    for j in range(W): -            block += 1 -print((h * w - d - block - 1)) +            ans -= 1 +ans -= d[-1][-1] + 1 +print(ans)