from collections import deque

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

S = [input().strip() for _ in range(H)]

cnt = 0

for i in range(H):

    for j in range(W):

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

            cnt += 1

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

que = deque([(0,0,0)])

dist[0][0] = 0

while que:

    i,j,d = que.popleft()

    if j+1<W and dist[i][j+1]<0 and S[i][j+1]==".":

        que.append((i,j+1,d+1))

        dist[i][j+1]=d+1

    if i-1>=0 and dist[i-1][j]<0 and S[i-1][j]==".":

        que.append((i-1,j,d+1))

        dist[i-1][j]=d+1

    if j-1>=0 and dist[i][j-1]<0 and S[i][j-1]==".":

        que.append((i,j-1,d+1))

        dist[i][j-1]=d+1

    if i+1<H and dist[i+1][j]<0 and S[i+1][j]==".":

        que.append((i+1,j,d+1))

        dist[i+1][j]=d+1

x = dist[H-1][W-1]

if x<0:

    print((-1))

else:

    print((cnt-1-x))
from collections import deque

INFTY = 10000

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

S = [input().strip() for _ in range(H)]

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

cnt = 0

for i in range(H):

    for j in range(W):

        if S[i][j]=="#":

            A[i][j]=INFTY

            cnt += 1

que = deque([(0,0,0)])

A[0][0] = 0

while que:

    i,j,d = que.popleft()

    if j+1<W and A[i][j+1]<0:

        A[i][j+1]=d+1

        que.append((i,j+1,d+1))

    if i-1>=0 and A[i-1][j]<0:

        A[i-1][j]=d+1

        que.append((i-1,j,d+1))

    if j-1>=0 and A[i][j-1]<0:

        A[i][j-1]=d+1

        que.append((i,j-1,d+1))

    if i+1<H and A[i+1][j]<0:

        A[i+1][j] = d+1

        que.append((i+1,j,d+1))

if A[H-1][W-1]<0:

    print((-1))

else:

    print((H*W-cnt-A[H-1][W-1]-1))
from collections import deque

H, W = list(map(int, input().split()))
S = [input().strip() for _ in range(H)]
cnt = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt += 1
dist = [[-1 for _ in range(W)] for _ in range(H)]
que = deque([(0, 0, 0)])
dist[0][0] = 0
while que:
    i, j, d = que.popleft()
    if j + 1 < W and dist[i][j + 1] < 0 and S[i][j + 1] == ".":
        que.append((i, j + 1, d + 1))
        dist[i][j + 1] = d + 1
    if i - 1 >= 0 and dist[i - 1][j] < 0 and S[i - 1][j] == ".":
        que.append((i - 1, j, d + 1))
        dist[i - 1][j] = d + 1
    if j - 1 >= 0 and dist[i][j - 1] < 0 and S[i][j - 1] == ".":
        que.append((i, j - 1, d + 1))
        dist[i][j - 1] = d + 1
    if i + 1 < H and dist[i + 1][j] < 0 and S[i + 1][j] == ".":
        que.append((i + 1, j, d + 1))
        dist[i + 1][j] = d + 1
x = dist[H - 1][W - 1]
if x < 0:
    print((-1))
else:
    print((cnt - 1 - x))

from collections import deque

INFTY = 10000
H, W = list(map(int, input().split()))
S = [input().strip() for _ in range(H)]
A = [[-1 for _ in range(W)] for _ in range(H)]
cnt = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            A[i][j] = INFTY
            cnt += 1
que = deque([(0, 0, 0)])
A[0][0] = 0
while que:
    i, j, d = que.popleft()
    if j + 1 < W and A[i][j + 1] < 0:
        A[i][j + 1] = d + 1
        que.append((i, j + 1, d + 1))
    if i - 1 >= 0 and A[i - 1][j] < 0:
        A[i - 1][j] = d + 1
        que.append((i - 1, j, d + 1))
    if j - 1 >= 0 and A[i][j - 1] < 0:
        A[i][j - 1] = d + 1
        que.append((i, j - 1, d + 1))
    if i + 1 < H and A[i + 1][j] < 0:
        A[i + 1][j] = d + 1
        que.append((i + 1, j, d + 1))
if A[H - 1][W - 1] < 0:
    print((-1))
else:
    print((H * W - cnt - A[H - 1][W - 1] - 1))

+INFTY = 10000 +A = [[-1 for _ in range(W)] for _ in range(H)] -        if S[i][j] == ".": +        if S[i][j] == "#": +            A[i][j] = INFTY -dist = [[-1 for _ in range(W)] for _ in range(H)] -dist[0][0] = 0 +A[0][0] = 0 -    if j + 1 < W and dist[i][j + 1] < 0 and S[i][j + 1] == ".": +    if j + 1 < W and A[i][j + 1] < 0: +        A[i][j + 1] = d + 1 -        dist[i][j + 1] = d + 1 -    if i - 1 >= 0 and dist[i - 1][j] < 0 and S[i - 1][j] == ".": +    if i - 1 >= 0 and A[i - 1][j] < 0: +        A[i - 1][j] = d + 1 -        dist[i - 1][j] = d + 1 -    if j - 1 >= 0 and dist[i][j - 1] < 0 and S[i][j - 1] == ".": +    if j - 1 >= 0 and A[i][j - 1] < 0: +        A[i][j - 1] = d + 1 -        dist[i][j - 1] = d + 1 -    if i + 1 < H and dist[i + 1][j] < 0 and S[i + 1][j] == ".": +    if i + 1 < H and A[i + 1][j] < 0: +        A[i + 1][j] = d + 1 -        dist[i + 1][j] = d + 1 -x = dist[H - 1][W - 1] -if x < 0: +if A[H - 1][W - 1] < 0: -    print((cnt - 1 - x)) +    print((H * W - cnt - A[H - 1][W - 1] - 1))