from collections import deque

dirc = ((1, 0), (0, 1), (-1, 0), (0, -1))

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

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

white = 0

for h in range(H):

    for w in range(W):

        if maze[h][w]=='.':

            white += 1

queue = deque()

queue.appendleft((0, (0, 0)))

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

ans = 0

while queue:

    c, (y, x) = queue.pop()

    c += 1

    if checked[y][x]:

        continue

    checked[y][x] = 1

    if (y, x)==(H-1, W-1):

        ans = c

        break

    for i, (dy, dx) in enumerate(dirc):

        ny, nx = y+dy, x+dx

        if nx<0 or nx>=W or ny<0 or ny>=H:

            continue

        if maze[ny][nx]=='#':

            continue

        if not checked[ny][nx]:

            queue.appendleft((c, (ny, nx)))

if ans==0:

    print((-1))

else:

    print((white-ans))
import sys

def input(): return sys.stdin.readline().strip()

def mapint(): return list(map(int, input().split()))

sys.setrecursionlimit(10**9)



H, W = mapint()

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

from collections import deque

Q = deque()



dirc = ((1, 0), (0, 1), (-1, 0), (0, -1))

Q.append((0, 0))

dist = [[10**18]*W for _ in range(H)]

dist[0][0] = 1

while Q:

    y, x = Q.popleft()

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

        break

    for dy, dx in dirc:

        ny, nx = dy+y, dx+x

        if ny<0 or ny>=H or nx<0 or nx>=W:

            continue

        if Ss[ny][nx]=='#':

            continue

        if dist[ny][nx]>dist[y][x]+1:

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

            Q.append((ny, nx))



white = 0

for s in Ss:

    white += s.count('.')

if dist[H-1][W-1]>=10**17:

    print((-1))

else:

    print((white-dist[H-1][W-1]))
from collections import deque

dirc = ((1, 0), (0, 1), (-1, 0), (0, -1))
H, W = list(map(int, input().split()))
maze = [list(str(eval(input()))) for _ in range(H)]
white = 0
for h in range(H):
    for w in range(W):
        if maze[h][w] == ".":
            white += 1
queue = deque()
queue.appendleft((0, (0, 0)))
checked = [[0] * W for _ in range(H)]
ans = 0
while queue:
    c, (y, x) = queue.pop()
    c += 1
    if checked[y][x]:
        continue
    checked[y][x] = 1
    if (y, x) == (H - 1, W - 1):
        ans = c
        break
    for i, (dy, dx) in enumerate(dirc):
        ny, nx = y + dy, x + dx
        if nx < 0 or nx >= W or ny < 0 or ny >= H:
            continue
        if maze[ny][nx] == "#":
            continue
        if not checked[ny][nx]:
            queue.appendleft((c, (ny, nx)))
if ans == 0:
    print((-1))
else:
    print((white - ans))

import sys


def input():
    return sys.stdin.readline().strip()


def mapint():
    return list(map(int, input().split()))


sys.setrecursionlimit(10**9)
H, W = mapint()
Ss = [eval(input()) for _ in range(H)]
from collections import deque

Q = deque()
dirc = ((1, 0), (0, 1), (-1, 0), (0, -1))
Q.append((0, 0))
dist = [[10**18] * W for _ in range(H)]
dist[0][0] = 1
while Q:
    y, x = Q.popleft()
    if y == H - 1 and x == W - 1:
        break
    for dy, dx in dirc:
        ny, nx = dy + y, dx + x
        if ny < 0 or ny >= H or nx < 0 or nx >= W:
            continue
        if Ss[ny][nx] == "#":
            continue
        if dist[ny][nx] > dist[y][x] + 1:
            dist[ny][nx] = dist[y][x] + 1
            Q.append((ny, nx))
white = 0
for s in Ss:
    white += s.count(".")
if dist[H - 1][W - 1] >= 10**17:
    print((-1))
else:
    print((white - dist[H - 1][W - 1]))

+import sys + + +def input(): +    return sys.stdin.readline().strip() + + +def mapint(): +    return list(map(int, input().split())) + + +sys.setrecursionlimit(10**9) +H, W = mapint() +Ss = [eval(input()) for _ in range(H)] +Q = deque() -H, W = list(map(int, input().split())) -maze = [list(str(eval(input()))) for _ in range(H)] +Q.append((0, 0)) +dist = [[10**18] * W for _ in range(H)] +dist[0][0] = 1 +while Q: +    y, x = Q.popleft() +    if y == H - 1 and x == W - 1: +        break +    for dy, dx in dirc: +        ny, nx = dy + y, dx + x +        if ny < 0 or ny >= H or nx < 0 or nx >= W: +            continue +        if Ss[ny][nx] == "#": +            continue +        if dist[ny][nx] > dist[y][x] + 1: +            dist[ny][nx] = dist[y][x] + 1 +            Q.append((ny, nx)) -for h in range(H): -    for w in range(W): -        if maze[h][w] == ".": -            white += 1 -queue = deque() -queue.appendleft((0, (0, 0))) -checked = [[0] * W for _ in range(H)] -ans = 0 -while queue: -    c, (y, x) = queue.pop() -    c += 1 -    if checked[y][x]: -        continue -    checked[y][x] = 1 -    if (y, x) == (H - 1, W - 1): -        ans = c -        break -    for i, (dy, dx) in enumerate(dirc): -        ny, nx = y + dy, x + dx -        if nx < 0 or nx >= W or ny < 0 or ny >= H: -            continue -        if maze[ny][nx] == "#": -            continue -        if not checked[ny][nx]: -            queue.appendleft((c, (ny, nx))) -if ans == 0: +for s in Ss: +    white += s.count(".") +if dist[H - 1][W - 1] >= 10**17: -    print((white - ans)) +    print((white - dist[H - 1][W - 1]))