from collections import defaultdict

from collections import deque

h,w=list(map(int,input().split()))

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

field=[[float('inf') for i in range(w)] for j in range(h)]



q=deque()

q.append((0,0))

field[0][0]=0



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

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



while q:

    y,x=q.popleft()

    

    for i in range(4):

        nx=x+dx[i]

        ny=y+dy[i]

        if 0<=nx<w and 0<=ny<h and field[ny][nx]==float('inf') and s[ny][nx]!='#':

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

            q.append((ny,nx))

check=field[h-1][w-1]



dic=defaultdict(int)

for i in range(h):

    for j in range(w):

        dic[field[i][j]]+=1

ans=0



for key,value in list(dic.items()):

    if key!=float('inf') and key <=check:

        ans+=value-1

    elif key!=float('inf') and key >check:

        ans+=value

    



for i in range(h):

    for j in range(w):

        if field[i][j]==float('inf') and s[i][j]=='.':

            ans+=1

print((ans if check!=float('inf') else -1))



from collections import defaultdict

from collections import deque

h,w=list(map(int,input().split()))

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

field=[[float('inf') for i in range(w)] for j in range(h)]



q=deque()

q.append((0,0))

field[0][0]=0



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

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



while q:

    y,x=q.popleft()

    

    for i in range(4):

        nx=x+dx[i]

        ny=y+dy[i]

        if 0<=nx<w and 0<=ny<h and field[ny][nx]==float('inf') and s[ny][nx]!='#':

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

            q.append((ny,nx))

check=field[h-1][w-1]



shiro=0

for i in range(h):

    for j in range(w):

        if s[i][j]=='.':

            shiro+=1

ans=shiro-check-1



print((ans if check!=float('inf') else -1))



from collections import defaultdict
from collections import deque

h, w = list(map(int, input().split()))
s = [list(eval(input())) for i in range(h)]
field = [[float("inf") for i in range(w)] for j in range(h)]
q = deque()
q.append((0, 0))
field[0][0] = 0
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
while q:
    y, x = q.popleft()
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if (
            0 <= nx < w
            and 0 <= ny < h
            and field[ny][nx] == float("inf")
            and s[ny][nx] != "#"
        ):
            field[ny][nx] = field[y][x] + 1
            q.append((ny, nx))
check = field[h - 1][w - 1]
dic = defaultdict(int)
for i in range(h):
    for j in range(w):
        dic[field[i][j]] += 1
ans = 0
for key, value in list(dic.items()):
    if key != float("inf") and key <= check:
        ans += value - 1
    elif key != float("inf") and key > check:
        ans += value
for i in range(h):
    for j in range(w):
        if field[i][j] == float("inf") and s[i][j] == ".":
            ans += 1
print((ans if check != float("inf") else -1))

from collections import defaultdict
from collections import deque

h, w = list(map(int, input().split()))
s = [list(eval(input())) for i in range(h)]
field = [[float("inf") for i in range(w)] for j in range(h)]
q = deque()
q.append((0, 0))
field[0][0] = 0
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
while q:
    y, x = q.popleft()
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if (
            0 <= nx < w
            and 0 <= ny < h
            and field[ny][nx] == float("inf")
            and s[ny][nx] != "#"
        ):
            field[ny][nx] = field[y][x] + 1
            q.append((ny, nx))
check = field[h - 1][w - 1]
shiro = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            shiro += 1
ans = shiro - check - 1
print((ans if check != float("inf") else -1))

-dic = defaultdict(int) +shiro = 0 -        dic[field[i][j]] += 1 -ans = 0 -for key, value in list(dic.items()): -    if key != float("inf") and key <= check: -        ans += value - 1 -    elif key != float("inf") and key > check: -        ans += value -for i in range(h): -    for j in range(w): -        if field[i][j] == float("inf") and s[i][j] == ".": -            ans += 1 +        if s[i][j] == ".": +            shiro += 1 +ans = shiro - check - 1