from _collections import deque

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

T = []

for i in range(H):

    a = list(eval(input()))

    T.append(a)



K = [[0,0,0]]

P = deque(K)

dp = [[10**6]*W for _ in range(H)]

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

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



while(len(P)>0):

    b = P.popleft()

    h = b[0]

    w = b[1]

    c = b[2]

    if c < dp[H-1][W-1]:

        for i,j in move:

            if h+i >= 0 and h+i <= H-1:

                if w+j >= 0 and w+j <= W-1:

                    if T[h+i][w+j] == ".":

                        dp[h+i][w+j] = min(c+1,dp[h+i][w+j])

                        if used[h+i][w+j] == 0:

                            used[h+i][w+j] = 1

                            P.append([h+i,w+j,c+1])



ans = 0

for i in range(H):

    for j in range(W):

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

            ans += 1

if dp[H-1][W-1] == 10**6:

    print((-1))

else:

    print((ans-dp[H-1][W-1]-1))
from collections import deque

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

K = []

for i in range(H):

    A = list(eval(input()))

    K.append(A)

P = deque([[0, 0, 1]])

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

used[0][0] = 1

while(True):

    if len(P) == 0:

        break

    h, w, c = P.popleft()

    if h == H-1 and w == W-1:

        break

    for i, j in [(1, 0), (-1, 0), (0, 1), (0, -1)]:

        if 0 <= h+i <= H-1:

            if 0 <= w+j <= W-1:

                if K[h+i][w+j] == ".":

                    if used[h+i][w+j] == 0:

                        used[h+i][w+j] = 1

                        P.append([h+i, w+j, c+1])



ans = 0

for i in range(H):

    for j in range(W):

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

            ans += 1

if h == H-1 and w == W-1:

    print((ans - c))

else:

    print((-1))
from _collections import deque

H, W = list(map(int, input().split()))
T = []
for i in range(H):
    a = list(eval(input()))
    T.append(a)
K = [[0, 0, 0]]
P = deque(K)
dp = [[10**6] * W for _ in range(H)]
used = [[0] * W for _ in range(H)]
move = [(-1, 0), (0, -1), (1, 0), (0, 1)]
while len(P) > 0:
    b = P.popleft()
    h = b[0]
    w = b[1]
    c = b[2]
    if c < dp[H - 1][W - 1]:
        for i, j in move:
            if h + i >= 0 and h + i <= H - 1:
                if w + j >= 0 and w + j <= W - 1:
                    if T[h + i][w + j] == ".":
                        dp[h + i][w + j] = min(c + 1, dp[h + i][w + j])
                        if used[h + i][w + j] == 0:
                            used[h + i][w + j] = 1
                            P.append([h + i, w + j, c + 1])
ans = 0
for i in range(H):
    for j in range(W):
        if T[i][j] == ".":
            ans += 1
if dp[H - 1][W - 1] == 10**6:
    print((-1))
else:
    print((ans - dp[H - 1][W - 1] - 1))

from collections import deque

H, W = list(map(int, input().split()))
K = []
for i in range(H):
    A = list(eval(input()))
    K.append(A)
P = deque([[0, 0, 1]])
used = [[0] * W for _ in range(H)]
used[0][0] = 1
while True:
    if len(P) == 0:
        break
    h, w, c = P.popleft()
    if h == H - 1 and w == W - 1:
        break
    for i, j in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        if 0 <= h + i <= H - 1:
            if 0 <= w + j <= W - 1:
                if K[h + i][w + j] == ".":
                    if used[h + i][w + j] == 0:
                        used[h + i][w + j] = 1
                        P.append([h + i, w + j, c + 1])
ans = 0
for i in range(H):
    for j in range(W):
        if K[i][j] == ".":
            ans += 1
if h == H - 1 and w == W - 1:
    print((ans - c))
else:
    print((-1))

-from _collections import deque +from collections import deque -T = [] +K = [] -    a = list(eval(input())) -    T.append(a) -K = [[0, 0, 0]] -P = deque(K) -dp = [[10**6] * W for _ in range(H)] +    A = list(eval(input())) +    K.append(A) +P = deque([[0, 0, 1]]) -move = [(-1, 0), (0, -1), (1, 0), (0, 1)] -while len(P) > 0: -    b = P.popleft() -    h = b[0] -    w = b[1] -    c = b[2] -    if c < dp[H - 1][W - 1]: -        for i, j in move: -            if h + i >= 0 and h + i <= H - 1: -                if w + j >= 0 and w + j <= W - 1: -                    if T[h + i][w + j] == ".": -                        dp[h + i][w + j] = min(c + 1, dp[h + i][w + j]) -                        if used[h + i][w + j] == 0: -                            used[h + i][w + j] = 1 -                            P.append([h + i, w + j, c + 1]) +used[0][0] = 1 +while True: +    if len(P) == 0: +        break +    h, w, c = P.popleft() +    if h == H - 1 and w == W - 1: +        break +    for i, j in [(1, 0), (-1, 0), (0, 1), (0, -1)]: +        if 0 <= h + i <= H - 1: +            if 0 <= w + j <= W - 1: +                if K[h + i][w + j] == ".": +                    if used[h + i][w + j] == 0: +                        used[h + i][w + j] = 1 +                        P.append([h + i, w + j, c + 1]) -        if T[i][j] == ".": +        if K[i][j] == ".": -if dp[H - 1][W - 1] == 10**6: +if h == H - 1 and w == W - 1: +    print((ans - c)) +else: -else: -    print((ans - dp[H - 1][W - 1] - 1))