# -*- coding: utf-8 -*-

from collections import deque

def inpl(): return tuple(map(int, input().split()))



H, W = inpl()



S = []

for _ in range(H):

    S.append(eval(input()))



M = [[50*50*10 if S[h][w]=="." else -1 for w in range(W)] for h in range(H)]



Q = deque() #step, H, W

Q.append([1, 0, 0])



ok = False

while Q:

    s, h, w = Q.popleft()

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

        ok = True

        break

    

    if h > 0:

        if M[h-1][w] > s+1:

            M[h-1][w] = s+1

            Q.append([s+1, h-1, w])

    

    if h < H-1:

        if M[h+1][w] > s+1:

            M[h+1][w] = s+1

            Q.append([s+1, h+1, w])



    if w > 0:

        if M[h][w-1] > s+1:

            M[h][w-1] = s+1

            Q.append([s+1, h, w-1])

    

    if w < W-1:

        if M[h][w+1] > s+1:

            M[h][w+1] = s+1

            Q.append([s+1, h, w+1])



if ok:

    b = sum([s.count("#") for s in S])

    print((H*W - b - s))

else:

    print((-1))
# -*- coding: utf-8 -*-

from collections import deque

def inpl(): return tuple(map(int, input().split()))



H, W = inpl()



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

V = [[True if S[h][w]=="." else False for w in range(W)] for h in range(H)]

V[0][0] = True



Q = deque() #step, H, W

Q.append([1, 0, 0])



ok = False

while Q:

    s, h, w = Q.popleft()

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

        ok = True

        break

    

    if h > 0:

        if V[h-1][w]:

            V[h-1][w] = False

            Q.append([s+1, h-1, w])

    

    if h < H-1:

        if V[h+1][w]:

            V[h+1][w] = False

            Q.append([s+1, h+1, w])



    if w > 0:

        if V[h][w-1]:

            V[h][w-1] = False

            Q.append([s+1, h, w-1])

    

    if w < W-1:

        if V[h][w+1]:

            V[h][w+1] = False

            Q.append([s+1, h, w+1])



if ok:

    b = sum([s.count("#") for s in S])

    print((H*W - b - s))

else:

    print((-1))
# -*- coding: utf-8 -*-
from collections import deque


def inpl():
    return tuple(map(int, input().split()))


H, W = inpl()
S = []
for _ in range(H):
    S.append(eval(input()))
M = [[50 * 50 * 10 if S[h][w] == "." else -1 for w in range(W)] for h in range(H)]
Q = deque()  # step, H, W
Q.append([1, 0, 0])
ok = False
while Q:
    s, h, w = Q.popleft()
    if h == H - 1 and w == W - 1:
        ok = True
        break
    if h > 0:
        if M[h - 1][w] > s + 1:
            M[h - 1][w] = s + 1
            Q.append([s + 1, h - 1, w])
    if h < H - 1:
        if M[h + 1][w] > s + 1:
            M[h + 1][w] = s + 1
            Q.append([s + 1, h + 1, w])
    if w > 0:
        if M[h][w - 1] > s + 1:
            M[h][w - 1] = s + 1
            Q.append([s + 1, h, w - 1])
    if w < W - 1:
        if M[h][w + 1] > s + 1:
            M[h][w + 1] = s + 1
            Q.append([s + 1, h, w + 1])
if ok:
    b = sum([s.count("#") for s in S])
    print((H * W - b - s))
else:
    print((-1))

# -*- coding: utf-8 -*-
from collections import deque


def inpl():
    return tuple(map(int, input().split()))


H, W = inpl()
S = [eval(input()) for _ in range(H)]
V = [[True if S[h][w] == "." else False for w in range(W)] for h in range(H)]
V[0][0] = True
Q = deque()  # step, H, W
Q.append([1, 0, 0])
ok = False
while Q:
    s, h, w = Q.popleft()
    if h == H - 1 and w == W - 1:
        ok = True
        break
    if h > 0:
        if V[h - 1][w]:
            V[h - 1][w] = False
            Q.append([s + 1, h - 1, w])
    if h < H - 1:
        if V[h + 1][w]:
            V[h + 1][w] = False
            Q.append([s + 1, h + 1, w])
    if w > 0:
        if V[h][w - 1]:
            V[h][w - 1] = False
            Q.append([s + 1, h, w - 1])
    if w < W - 1:
        if V[h][w + 1]:
            V[h][w + 1] = False
            Q.append([s + 1, h, w + 1])
if ok:
    b = sum([s.count("#") for s in S])
    print((H * W - b - s))
else:
    print((-1))

-S = [] -for _ in range(H): -    S.append(eval(input())) -M = [[50 * 50 * 10 if S[h][w] == "." else -1 for w in range(W)] for h in range(H)] +S = [eval(input()) for _ in range(H)] +V = [[True if S[h][w] == "." else False for w in range(W)] for h in range(H)] +V[0][0] = True -        if M[h - 1][w] > s + 1: -            M[h - 1][w] = s + 1 +        if V[h - 1][w]: +            V[h - 1][w] = False -        if M[h + 1][w] > s + 1: -            M[h + 1][w] = s + 1 +        if V[h + 1][w]: +            V[h + 1][w] = False -        if M[h][w - 1] > s + 1: -            M[h][w - 1] = s + 1 +        if V[h][w - 1]: +            V[h][w - 1] = False -        if M[h][w + 1] > s + 1: -            M[h][w + 1] = s + 1 +        if V[h][w + 1]: +            V[h][w + 1] = False