from collections import deque

def MI(): return list(map(int, input().split()))

def bfs():

  dist=[[INF] * (W+1) for _ in range(H+1)]

  dist[sx][sy]=0

  que=deque([])

  que.append((sx,sy))

  di=(-1,0,1,0)

  dj=(0,-1,0,1)

  while que:

    (x,y)=que.popleft()

    if x==gx and y==gy:

      break

    for k in range(4):

      ni=x+di[k]

      nj=y+dj[k]

      if 0<=ni<H and 0<=nj<W:

        if Map[ni][nj]!='#':

          if dist[ni][nj]==INF:

            dist[ni][nj]=dist[x][y]+1

            que.append((ni,nj))

  return dist[gx][gy]

H,W=MI()

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

#Map2=[[input()] for _ in range(H)]

INF=float('inf')

(sx,sy)=(0,0)

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

res=bfs()

white=0

for i in range(H):

  for j in range(W):

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

      white+=1

if 0<res<INF:

  ans=white-res-1

  print(ans)

else:

  print((-1))
from collections import deque

def MI(): return list(map(int, input().split()))

INF=float('inf')

def bfs():

  que=deque()

  que.append((sx,sy))

  dist=[[INF] *W for _ in range(H)]

  dist[sx][sy]=0

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

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

  while que:

    x,y=que.popleft()

    if x==gx and y==gy:

      break

    for i in range(4):

      nx=x+dx[i]

      ny=y+dy[i]

      if 0<=nx<H and 0<=ny<W:

        if Map[nx][ny]!='#':

          if dist[nx][ny]==INF:

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

            que.append((nx,ny))

  return dist[gx][gy]

H,W=MI()

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

sx,sy=0,0

gx,gy=H-1,W-1

white=0

for i in range(H):

  for j in range(W):

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

      white+=1

res=bfs()

if 0<res<INF:

  print((white-res-1))

else:

  print((-1))
from collections import deque


def MI():
    return list(map(int, input().split()))


def bfs():
    dist = [[INF] * (W + 1) for _ in range(H + 1)]
    dist[sx][sy] = 0
    que = deque([])
    que.append((sx, sy))
    di = (-1, 0, 1, 0)
    dj = (0, -1, 0, 1)
    while que:
        (x, y) = que.popleft()
        if x == gx and y == gy:
            break
        for k in range(4):
            ni = x + di[k]
            nj = y + dj[k]
            if 0 <= ni < H and 0 <= nj < W:
                if Map[ni][nj] != "#":
                    if dist[ni][nj] == INF:
                        dist[ni][nj] = dist[x][y] + 1
                        que.append((ni, nj))
    return dist[gx][gy]


H, W = MI()
Map = [list(eval(input())) for _ in range(H)]
# Map2=[[input()] for _ in range(H)]
INF = float("inf")
(sx, sy) = (0, 0)
(gx, gy) = (H - 1, W - 1)
res = bfs()
white = 0
for i in range(H):
    for j in range(W):
        if Map[i][j] == ".":
            white += 1
if 0 < res < INF:
    ans = white - res - 1
    print(ans)
else:
    print((-1))

from collections import deque


def MI():
    return list(map(int, input().split()))


INF = float("inf")


def bfs():
    que = deque()
    que.append((sx, sy))
    dist = [[INF] * W for _ in range(H)]
    dist[sx][sy] = 0
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    while que:
        x, y = que.popleft()
        if x == gx and y == gy:
            break
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < H and 0 <= ny < W:
                if Map[nx][ny] != "#":
                    if dist[nx][ny] == INF:
                        dist[nx][ny] = dist[x][y] + 1
                        que.append((nx, ny))
    return dist[gx][gy]


H, W = MI()
Map = [list(eval(input())) for i in range(H)]
sx, sy = 0, 0
gx, gy = H - 1, W - 1
white = 0
for i in range(H):
    for j in range(W):
        if Map[i][j] == ".":
            white += 1
res = bfs()
if 0 < res < INF:
    print((white - res - 1))
else:
    print((-1))

+INF = float("inf") + + -    dist = [[INF] * (W + 1) for _ in range(H + 1)] +    que = deque() +    que.append((sx, sy)) +    dist = [[INF] * W for _ in range(H)] -    que = deque([]) -    que.append((sx, sy)) -    di = (-1, 0, 1, 0) -    dj = (0, -1, 0, 1) +    dx = [1, 0, -1, 0] +    dy = [0, 1, 0, -1] -        (x, y) = que.popleft() +        x, y = que.popleft() -        for k in range(4): -            ni = x + di[k] -            nj = y + dj[k] -            if 0 <= ni < H and 0 <= nj < W: -                if Map[ni][nj] != "#": -                    if dist[ni][nj] == INF: -                        dist[ni][nj] = dist[x][y] + 1 -                        que.append((ni, nj)) +        for i in range(4): +            nx = x + dx[i] +            ny = y + dy[i] +            if 0 <= nx < H and 0 <= ny < W: +                if Map[nx][ny] != "#": +                    if dist[nx][ny] == INF: +                        dist[nx][ny] = dist[x][y] + 1 +                        que.append((nx, ny)) -Map = [list(eval(input())) for _ in range(H)] -# Map2=[[input()] for _ in range(H)] -INF = float("inf") -(sx, sy) = (0, 0) -(gx, gy) = (H - 1, W - 1) -res = bfs() +Map = [list(eval(input())) for i in range(H)] +sx, sy = 0, 0 +gx, gy = H - 1, W - 1 +res = bfs() -    ans = white - res - 1 -    print(ans) +    print((white - res - 1))