import queue

INF=10**8

r,c=list(map(int,input().split()))

#sy,sx=map(int,input().split())

#gy,gx=map(int,input().split())

maze=[eval(input()) for _ in range(r)]

d=[[INF for i in range(r)] for _ in range(c)]

sx,sy=1,1

gx,gy=r,c

cnt=0

for i in range(r):

  for j in range(c):

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

      cnt+=1



sx-=1

sy-=1

gx-=1

gy-=1

q=queue.Queue()

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

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

q.put((sx,sy))

d[sy][sx]=0



while not q.empty():

  x,y=q.get()

  if (x,y)==(gx,gy):break

  for i in range(4):

    nx=x+dx[i]

    ny=y+dy[i]

    if 0<=nx<c and 0<=ny<r:

      if maze[ny][nx]=='.' and d[nx][ny]==INF:

        q.put((nx,ny))

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

if d[gy][gx]==INF:

  print((-1))

else:print((cnt-d[gy][gx]-1))
from collections import deque

INF=10**18

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

a=[eval(input()) for _ in range(h)]

d=[[INF for _ in range(w)]for _ in range(h)]

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

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

sx,sy=0,0

gx,gy=h-1,w-1

d[sx][sy]=1

q=deque([(sx,sy)])

while q:

  x,y=q.popleft()

  if (x,y)==(gx,gy):break

  for i in range(4):

    nx,ny=x+dx[i],y+dy[i]

    if 0<=nx<h and 0<=ny<w:

      if a[nx][ny]=='.' and d[nx][ny]==INF:

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

        q.append((nx,ny))

if d[gx][gy]==INF:print((-1))

else:

  cnt=0

  for i in a:

    cnt+=i.count('.')

  print((cnt-d[gx][gy]))
import queue

INF = 10**8
r, c = list(map(int, input().split()))
# sy,sx=map(int,input().split())
# gy,gx=map(int,input().split())
maze = [eval(input()) for _ in range(r)]
d = [[INF for i in range(r)] for _ in range(c)]
sx, sy = 1, 1
gx, gy = r, c
cnt = 0
for i in range(r):
    for j in range(c):
        if maze[i][j] == ".":
            cnt += 1
sx -= 1
sy -= 1
gx -= 1
gy -= 1
q = queue.Queue()
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
q.put((sx, sy))
d[sy][sx] = 0
while not q.empty():
    x, y = q.get()
    if (x, y) == (gx, gy):
        break
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx < c and 0 <= ny < r:
            if maze[ny][nx] == "." and d[nx][ny] == INF:
                q.put((nx, ny))
                d[nx][ny] = d[x][y] + 1
if d[gy][gx] == INF:
    print((-1))
else:
    print((cnt - d[gy][gx] - 1))

from collections import deque

INF = 10**18
h, w = list(map(int, input().split()))
a = [eval(input()) for _ in range(h)]
d = [[INF for _ in range(w)] for _ in range(h)]
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
sx, sy = 0, 0
gx, gy = h - 1, w - 1
d[sx][sy] = 1
q = deque([(sx, sy)])
while q:
    x, y = q.popleft()
    if (x, y) == (gx, gy):
        break
    for i in range(4):
        nx, ny = x + dx[i], y + dy[i]
        if 0 <= nx < h and 0 <= ny < w:
            if a[nx][ny] == "." and d[nx][ny] == INF:
                d[nx][ny] = d[x][y] + 1
                q.append((nx, ny))
if d[gx][gy] == INF:
    print((-1))
else:
    cnt = 0
    for i in a:
        cnt += i.count(".")
    print((cnt - d[gx][gy]))

-import queue +from collections import deque -INF = 10**8 -r, c = list(map(int, input().split())) -# sy,sx=map(int,input().split()) -# gy,gx=map(int,input().split()) -maze = [eval(input()) for _ in range(r)] -d = [[INF for i in range(r)] for _ in range(c)] -sx, sy = 1, 1 -gx, gy = r, c -cnt = 0 -for i in range(r): -    for j in range(c): -        if maze[i][j] == ".": -            cnt += 1 -sx -= 1 -sy -= 1 -gx -= 1 -gy -= 1 -q = queue.Queue() -dx = [-1, 0, 1, 0] -dy = [0, -1, 0, 1] -q.put((sx, sy)) -d[sy][sx] = 0 -while not q.empty(): -    x, y = q.get() +INF = 10**18 +h, w = list(map(int, input().split())) +a = [eval(input()) for _ in range(h)] +d = [[INF for _ in range(w)] for _ in range(h)] +dx = [0, 1, 0, -1] +dy = [1, 0, -1, 0] +sx, sy = 0, 0 +gx, gy = h - 1, w - 1 +d[sx][sy] = 1 +q = deque([(sx, sy)]) +while q: +    x, y = q.popleft() -        nx = x + dx[i] -        ny = y + dy[i] -        if 0 <= nx < c and 0 <= ny < r: -            if maze[ny][nx] == "." and d[nx][ny] == INF: -                q.put((nx, ny)) +        nx, ny = x + dx[i], y + dy[i] +        if 0 <= nx < h and 0 <= ny < w: +            if a[nx][ny] == "." and d[nx][ny] == INF: -if d[gy][gx] == INF: +                q.append((nx, ny)) +if d[gx][gy] == INF: -    print((cnt - d[gy][gx] - 1)) +    cnt = 0 +    for i in a: +        cnt += i.count(".") +    print((cnt - d[gx][gy]))