from collections import deque

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

INF = float("inf")

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

T = [[""] * W for _ in range(H)]

sx, sy = 0, 0

gx, gy = H - 1, W - 1

num = 0

for i in range(H):

    a = eval(input())

    for j in range(W):

        T[i][j] = a[j]

    num += a.count("#")





def bfs():

    que = deque()

    que.append((sx, sy))

    d[sx][sy] = 0



    while len(que):

        x, y = que.popleft()

        if x == gx and y == gy:

            break

        for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:

            nx, ny = x + dx, y + dy



            if 0 <= nx < H and 0 <= ny < W and T[nx][ny] != "#" and d[nx][ny] == INF:

                que.append((nx, ny))

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



    return d[gx][gy]





ans = bfs()

if ans < INF:

    print((H * W - ans - num - 1))

else:

    print((-1))

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

T = [[] for i in range(H)]

b = 0

for i in range(H):

    T[i] = list(eval(input()))

    for j in range(W):

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

            b += 1



que = [(0, 0, 1)]

while que:

    x, y, k = que.pop(0)

    if x == H - 1 and y == W - 1:

        print((H * W - b - k))

        exit()



    for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:

        nx, ny = x + dx, y + dy

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

            T[nx][ny] = '#'

            que.append((nx, ny, k + 1))

print((-1))
from collections import deque

H, W = list(map(int, input().split()))
INF = float("inf")
d = [[INF] * W for _ in range(H)]
T = [[""] * W for _ in range(H)]
sx, sy = 0, 0
gx, gy = H - 1, W - 1
num = 0
for i in range(H):
    a = eval(input())
    for j in range(W):
        T[i][j] = a[j]
    num += a.count("#")


def bfs():
    que = deque()
    que.append((sx, sy))
    d[sx][sy] = 0
    while len(que):
        x, y = que.popleft()
        if x == gx and y == gy:
            break
        for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
            nx, ny = x + dx, y + dy
            if 0 <= nx < H and 0 <= ny < W and T[nx][ny] != "#" and d[nx][ny] == INF:
                que.append((nx, ny))
                d[nx][ny] = d[x][y] + 1
    return d[gx][gy]


ans = bfs()
if ans < INF:
    print((H * W - ans - num - 1))
else:
    print((-1))

H, W = list(map(int, input().split()))
T = [[] for i in range(H)]
b = 0
for i in range(H):
    T[i] = list(eval(input()))
    for j in range(W):
        if T[i][j] == "#":
            b += 1
que = [(0, 0, 1)]
while que:
    x, y, k = que.pop(0)
    if x == H - 1 and y == W - 1:
        print((H * W - b - k))
        exit()
    for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
        nx, ny = x + dx, y + dy
        if 0 <= nx < H and 0 <= ny < W and T[nx][ny] == ".":
            T[nx][ny] = "#"
            que.append((nx, ny, k + 1))
print((-1))

-from collections import deque - -INF = float("inf") -d = [[INF] * W for _ in range(H)] -T = [[""] * W for _ in range(H)] -sx, sy = 0, 0 -gx, gy = H - 1, W - 1 -num = 0 +T = [[] for i in range(H)] +b = 0 -    a = eval(input()) +    T[i] = list(eval(input())) -        T[i][j] = a[j] -    num += a.count("#") - - -def bfs(): -    que = deque() -    que.append((sx, sy)) -    d[sx][sy] = 0 -    while len(que): -        x, y = que.popleft() -        if x == gx and y == gy: -            break -        for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]: -            nx, ny = x + dx, y + dy -            if 0 <= nx < H and 0 <= ny < W and T[nx][ny] != "#" and d[nx][ny] == INF: -                que.append((nx, ny)) -                d[nx][ny] = d[x][y] + 1 -    return d[gx][gy] - - -ans = bfs() -if ans < INF: -    print((H * W - ans - num - 1)) -else: -    print((-1)) +        if T[i][j] == "#": +            b += 1 +que = [(0, 0, 1)] +while que: +    x, y, k = que.pop(0) +    if x == H - 1 and y == W - 1: +        print((H * W - b - k)) +        exit() +    for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]: +        nx, ny = x + dx, y + dy +        if 0 <= nx < H and 0 <= ny < W and T[nx][ny] == ".": +            T[nx][ny] = "#" +            que.append((nx, ny, k + 1)) +print((-1))