import queue

INF = 1 << 30

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

que = [(0, 0)]

S = [eval(input()) for i in range(H)]

flag = [[INF for i in range(W)] for j in range(H)]

flag[0][0] = 1

cnt = sum(row.count('.') for row in S)

while que:

	y, x = que[0]

	que.pop(0)

	for ny, nx in [1, 0], [-1, 0], [0, 1], [0, -1]:

		nx += x

		ny += y

		if ny >= 0 and nx >= 0 and ny < H and nx < W and S[ny][nx] != '#' and flag[ny][nx] > flag[y][x] + 1:

			que.append((ny, nx))

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

			if (ny, nx) == (H - 1, W - 1):

				print((cnt - flag[ny][nx]))

				exit()

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

que = [(0, 0)]

S = [eval(input()) for i in range(H)]

flag = [[1<<30 for i in range(W)] for j in range(H)]

flag[0][0] = 1

cnt = sum(row.count('.') for row in S)

while que:

	y, x = que.pop(0)

	for ny, nx in [1, 0], [-1, 0], [0, 1], [0, -1]:

		nx += x

		ny += y

		if ny >= 0 and nx >= 0 and ny < H and nx < W and S[ny][nx] != '#' and flag[ny][nx] > flag[y][x] + 1:

			que.append((ny, nx))

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

			if (ny, nx) == (H - 1, W - 1):

				print((cnt - flag[ny][nx]))

				exit()

print((-1))
import queue

INF = 1 << 30
H, W = list(map(int, input().split()))
que = [(0, 0)]
S = [eval(input()) for i in range(H)]
flag = [[INF for i in range(W)] for j in range(H)]
flag[0][0] = 1
cnt = sum(row.count(".") for row in S)
while que:
    y, x = que[0]
    que.pop(0)
    for ny, nx in [1, 0], [-1, 0], [0, 1], [0, -1]:
        nx += x
        ny += y
        if (
            ny >= 0
            and nx >= 0
            and ny < H
            and nx < W
            and S[ny][nx] != "#"
            and flag[ny][nx] > flag[y][x] + 1
        ):
            que.append((ny, nx))
            flag[ny][nx] = flag[y][x] + 1
            if (ny, nx) == (H - 1, W - 1):
                print((cnt - flag[ny][nx]))
                exit()
print((-1))

H, W = list(map(int, input().split()))
que = [(0, 0)]
S = [eval(input()) for i in range(H)]
flag = [[1 << 30 for i in range(W)] for j in range(H)]
flag[0][0] = 1
cnt = sum(row.count(".") for row in S)
while que:
    y, x = que.pop(0)
    for ny, nx in [1, 0], [-1, 0], [0, 1], [0, -1]:
        nx += x
        ny += y
        if (
            ny >= 0
            and nx >= 0
            and ny < H
            and nx < W
            and S[ny][nx] != "#"
            and flag[ny][nx] > flag[y][x] + 1
        ):
            que.append((ny, nx))
            flag[ny][nx] = flag[y][x] + 1
            if (ny, nx) == (H - 1, W - 1):
                print((cnt - flag[ny][nx]))
                exit()
print((-1))

-import queue - -INF = 1 << 30 -flag = [[INF for i in range(W)] for j in range(H)] +flag = [[1 << 30 for i in range(W)] for j in range(H)] -    y, x = que[0] -    que.pop(0) +    y, x = que.pop(0)