from collections import deque

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

s=[]

for i in range(H):

    si=eval(input())

    s.append(si)



queue=deque()

sy,sx=(0,0)

gy,gx=(H-1,W-1)

reached=[[-1 for i in range(W)] for j in range(H)]

queue.append([sy,sx])

reached[sy][sx]=0

dx=[1,-1,0,0]

dy=[0,0,1,-1]

while(queue):

    p=queue.pop()

    for i in range(4):

        y=p[0]+dy[i]

        x=p[1]+dx[i]

        if x<0 or x>=W or y<0 or y>=H or s[y][x]=="#":

            continue

        if reached[y][x]!=-1:

            continue

        reached[y][x]=reached[p[0]][p[1]]+1

        queue.appendleft([y,x])

n=H*W

for i in range(H):

    for j in range(W):

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

            n-=1

if reached[gy][gx]==-1:

  n=-1       

print((n-reached[gy][gx]-1))
from collections import deque

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

stack=deque()

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

ans=0

for ci in c:

    for j in ci:

        if j==".":

            ans+=1

reached=[[0]*W for i in range(H)]

stack.append((0,0))

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

while(stack):

    Y,X=stack.popleft()

    for dy,dx in dydx:

        y=Y+dy

        x=X+dx

        if y<0 or y>=H or x<0 or x>=W or c[y][x]=="#":

            continue

        if reached[y][x]!=0:

            continue

        stack.append((y,x))

        reached[y][x]=reached[Y][X]+1

if reached[H-1][W-1]==0:

    print((-1))

else:

    print((ans-(reached[H-1][W-1]+1)))

from collections import deque

H, W = list(map(int, input().split()))
s = []
for i in range(H):
    si = eval(input())
    s.append(si)
queue = deque()
sy, sx = (0, 0)
gy, gx = (H - 1, W - 1)
reached = [[-1 for i in range(W)] for j in range(H)]
queue.append([sy, sx])
reached[sy][sx] = 0
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
while queue:
    p = queue.pop()
    for i in range(4):
        y = p[0] + dy[i]
        x = p[1] + dx[i]
        if x < 0 or x >= W or y < 0 or y >= H or s[y][x] == "#":
            continue
        if reached[y][x] != -1:
            continue
        reached[y][x] = reached[p[0]][p[1]] + 1
        queue.appendleft([y, x])
n = H * W
for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            n -= 1
if reached[gy][gx] == -1:
    n = -1
print((n - reached[gy][gx] - 1))

from collections import deque

H, W = list(map(int, input().split()))
stack = deque()
c = [eval(input()) for i in range(H)]
ans = 0
for ci in c:
    for j in ci:
        if j == ".":
            ans += 1
reached = [[0] * W for i in range(H)]
stack.append((0, 0))
dydx = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while stack:
    Y, X = stack.popleft()
    for dy, dx in dydx:
        y = Y + dy
        x = X + dx
        if y < 0 or y >= H or x < 0 or x >= W or c[y][x] == "#":
            continue
        if reached[y][x] != 0:
            continue
        stack.append((y, x))
        reached[y][x] = reached[Y][X] + 1
if reached[H - 1][W - 1] == 0:
    print((-1))
else:
    print((ans - (reached[H - 1][W - 1] + 1)))

-s = [] -for i in range(H): -    si = eval(input()) -    s.append(si) -queue = deque() -sy, sx = (0, 0) -gy, gx = (H - 1, W - 1) -reached = [[-1 for i in range(W)] for j in range(H)] -queue.append([sy, sx]) -reached[sy][sx] = 0 -dx = [1, -1, 0, 0] -dy = [0, 0, 1, -1] -while queue: -    p = queue.pop() -    for i in range(4): -        y = p[0] + dy[i] -        x = p[1] + dx[i] -        if x < 0 or x >= W or y < 0 or y >= H or s[y][x] == "#": +stack = deque() +c = [eval(input()) for i in range(H)] +ans = 0 +for ci in c: +    for j in ci: +        if j == ".": +            ans += 1 +reached = [[0] * W for i in range(H)] +stack.append((0, 0)) +dydx = [(1, 0), (-1, 0), (0, 1), (0, -1)] +while stack: +    Y, X = stack.popleft() +    for dy, dx in dydx: +        y = Y + dy +        x = X + dx +        if y < 0 or y >= H or x < 0 or x >= W or c[y][x] == "#": -        if reached[y][x] != -1: +        if reached[y][x] != 0: -        reached[y][x] = reached[p[0]][p[1]] + 1 -        queue.appendleft([y, x]) -n = H * W -for i in range(H): -    for j in range(W): -        if s[i][j] == "#": -            n -= 1 -if reached[gy][gx] == -1: -    n = -1 -print((n - reached[gy][gx] - 1)) +        stack.append((y, x)) +        reached[y][x] = reached[Y][X] + 1 +if reached[H - 1][W - 1] == 0: +    print((-1)) +else: +    print((ans - (reached[H - 1][W - 1] + 1)))