from collections import deque

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

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



white = 0



for i in range(H):

    white += s[i].count(".")



q = deque()

q.append((0, 0))



g = [[10**9]*W for i in range(H)]

g[0][0] = 0

for i in range(H*W):

    if len(q) == 0:

        break

    x, y = q.popleft()

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

        break

    else:

        if x < H-1 and s[x+1][y] == ".":

            if g[x+1][y] == 10**9:

                q.append((x+1, y))

            g[x+1][y] = min(g[x+1][y], g[x][y]+1)

        if y < W-1 and s[x][y+1] == ".":

            if g[x][y+1] == 10**9:

                q.append((x, y+1))

            g[x][y+1] = min(g[x][y+1], g[x][y]+1)

        if x > 0 and s[x-1][y] == ".":

            if g[x-1][y] == 10**9:

                q.append((x-1, y))

            g[x-1][y] = min(g[x-1][y], g[x][y]+1)

        if y > 0 and s[x][y-1] == ".":

            if g[x][y-1] == 10**9:

                q.append((x, y-1))

            g[x][y-1] = min(g[x][y-1], g[x][y]+1)

if g[H-1][W-1] == 10**9:

    print((-1))

else:

    print((white - (g[H-1][W-1]+1)))

import collections

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

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



ans = H*W - 2

for i in range(H):

    for j in range(W):

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

            ans -= 1

q = collections.deque()

q.append((0, 0, 0))

flag = False

while(q):

    x, y, d = q.popleft()

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

        ans -= (d-1)

        flag = True

        break

    if y > 0 and s[y-1][x] == ".":

        q.append((x, y-1, d+1))

        s[y-1][x] = "#"

    if x > 0 and s[y][x-1] == ".":

        q.append((x-1, y, d+1))

        s[y][x-1] = "#"

    if y < H - 1 and s[y+1][x] == ".":

        q.append((x, y+1, d+1))

        s[y+1][x] = "#"

    if x < W - 1 and s[y][x+1] == ".":

        q.append((x+1, y, d+1))

        s[y][x+1] = "#"



if flag:

    print(ans)

else:

    print((-1))

from collections import deque

H, W = list(map(int, input().split()))
s = [list(eval(input())) for i in range(H)]
white = 0
for i in range(H):
    white += s[i].count(".")
q = deque()
q.append((0, 0))
g = [[10**9] * W for i in range(H)]
g[0][0] = 0
for i in range(H * W):
    if len(q) == 0:
        break
    x, y = q.popleft()
    if (x, y) == (H - 1, W - 1):
        break
    else:
        if x < H - 1 and s[x + 1][y] == ".":
            if g[x + 1][y] == 10**9:
                q.append((x + 1, y))
            g[x + 1][y] = min(g[x + 1][y], g[x][y] + 1)
        if y < W - 1 and s[x][y + 1] == ".":
            if g[x][y + 1] == 10**9:
                q.append((x, y + 1))
            g[x][y + 1] = min(g[x][y + 1], g[x][y] + 1)
        if x > 0 and s[x - 1][y] == ".":
            if g[x - 1][y] == 10**9:
                q.append((x - 1, y))
            g[x - 1][y] = min(g[x - 1][y], g[x][y] + 1)
        if y > 0 and s[x][y - 1] == ".":
            if g[x][y - 1] == 10**9:
                q.append((x, y - 1))
            g[x][y - 1] = min(g[x][y - 1], g[x][y] + 1)
if g[H - 1][W - 1] == 10**9:
    print((-1))
else:
    print((white - (g[H - 1][W - 1] + 1)))

import collections

H, W = list(map(int, input().split()))
s = [list(eval(input())) for i in range(H)]
ans = H * W - 2
for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            ans -= 1
q = collections.deque()
q.append((0, 0, 0))
flag = False
while q:
    x, y, d = q.popleft()
    if x == W - 1 and y == H - 1:
        ans -= d - 1
        flag = True
        break
    if y > 0 and s[y - 1][x] == ".":
        q.append((x, y - 1, d + 1))
        s[y - 1][x] = "#"
    if x > 0 and s[y][x - 1] == ".":
        q.append((x - 1, y, d + 1))
        s[y][x - 1] = "#"
    if y < H - 1 and s[y + 1][x] == ".":
        q.append((x, y + 1, d + 1))
        s[y + 1][x] = "#"
    if x < W - 1 and s[y][x + 1] == ".":
        q.append((x + 1, y, d + 1))
        s[y][x + 1] = "#"
if flag:
    print(ans)
else:
    print((-1))

-from collections import deque +import collections -white = 0 +ans = H * W - 2 -    white += s[i].count(".") -q = deque() -q.append((0, 0)) -g = [[10**9] * W for i in range(H)] -g[0][0] = 0 -for i in range(H * W): -    if len(q) == 0: +    for j in range(W): +        if s[i][j] == "#": +            ans -= 1 +q = collections.deque() +q.append((0, 0, 0)) +flag = False +while q: +    x, y, d = q.popleft() +    if x == W - 1 and y == H - 1: +        ans -= d - 1 +        flag = True -    x, y = q.popleft() -    if (x, y) == (H - 1, W - 1): -        break -    else: -        if x < H - 1 and s[x + 1][y] == ".": -            if g[x + 1][y] == 10**9: -                q.append((x + 1, y)) -            g[x + 1][y] = min(g[x + 1][y], g[x][y] + 1) -        if y < W - 1 and s[x][y + 1] == ".": -            if g[x][y + 1] == 10**9: -                q.append((x, y + 1)) -            g[x][y + 1] = min(g[x][y + 1], g[x][y] + 1) -        if x > 0 and s[x - 1][y] == ".": -            if g[x - 1][y] == 10**9: -                q.append((x - 1, y)) -            g[x - 1][y] = min(g[x - 1][y], g[x][y] + 1) -        if y > 0 and s[x][y - 1] == ".": -            if g[x][y - 1] == 10**9: -                q.append((x, y - 1)) -            g[x][y - 1] = min(g[x][y - 1], g[x][y] + 1) -if g[H - 1][W - 1] == 10**9: +    if y > 0 and s[y - 1][x] == ".": +        q.append((x, y - 1, d + 1)) +        s[y - 1][x] = "#" +    if x > 0 and s[y][x - 1] == ".": +        q.append((x - 1, y, d + 1)) +        s[y][x - 1] = "#" +    if y < H - 1 and s[y + 1][x] == ".": +        q.append((x, y + 1, d + 1)) +        s[y + 1][x] = "#" +    if x < W - 1 and s[y][x + 1] == ".": +        q.append((x + 1, y, d + 1)) +        s[y][x + 1] = "#" +if flag: +    print(ans) +else: -else: -    print((white - (g[H - 1][W - 1] + 1)))