import sys

from collections import deque

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

map = [[i for i in map(str,sys.stdin.readline().rstrip())] for _ in range(H)]

count_dot = 0

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

for i in range(H):

  for j in range(W):

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

      count_dot +=1

def bfs():

  while q:

    x,y = q.popleft()

    if x == H-1 and y ==W-1:

      break

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

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

    for i in range(4):

      nx = x + dx[i]

      ny = y + dy[i]

      if 0 <= nx < H and 0 <= ny < W and map[nx][ny]=='.' and dist[nx][ny] == 0:

        q.append((nx,ny))

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

q = deque()

q.append((0,0))

bfs()

dist_min = dist[H-1][W-1]

if dist_min ==0:

  print((-1))

else:

  print((count_dot-dist_min-1))
import sys 

from collections import deque

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

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

grid = ['#']*(W+2)

for _ in range(H):

  grid += ['#'] + list(map(str,readline().decode().rstrip())) + ['X']

grid += ['#']*(W+2)

 

dist = [-1]*((H+2)*(W+2)-1)

 

s = W + 3

g = (H+1)*(W+2)-2

 

def bfs():

  while q:

    x = q.popleft()

    if x == g:

      return

    for dx in (1,W+2,-1,-W-2):

      if dist[x+dx] == -1 and grid[x+dx] =='.':

        dist[x+dx] = dist[x]+1

        q.append(x+dx)



dist[s]=0

q = deque([(s)])

bfs()



if dist[g] ==-1:

  print((-1))

else:

  print((grid.count('.')-dist[g]-1))
import sys
from collections import deque

H, W = list(map(int, input().split()))
map = [[i for i in map(str, sys.stdin.readline().rstrip())] for _ in range(H)]
count_dot = 0
dist = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if map[i][j] == ".":
            count_dot += 1


def bfs():
    while q:
        x, y = q.popleft()
        if x == H - 1 and y == W - 1:
            break
        dx = [0, 1, 0, -1]
        dy = [1, 0, -1, 0]
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < H and 0 <= ny < W and map[nx][ny] == "." and dist[nx][ny] == 0:
                q.append((nx, ny))
                dist[nx][ny] = dist[x][y] + 1


q = deque()
q.append((0, 0))
bfs()
dist_min = dist[H - 1][W - 1]
if dist_min == 0:
    print((-1))
else:
    print((count_dot - dist_min - 1))

import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
H, W = list(map(int, readline().split()))
grid = ["#"] * (W + 2)
for _ in range(H):
    grid += ["#"] + list(map(str, readline().decode().rstrip())) + ["X"]
grid += ["#"] * (W + 2)
dist = [-1] * ((H + 2) * (W + 2) - 1)
s = W + 3
g = (H + 1) * (W + 2) - 2


def bfs():
    while q:
        x = q.popleft()
        if x == g:
            return
        for dx in (1, W + 2, -1, -W - 2):
            if dist[x + dx] == -1 and grid[x + dx] == ".":
                dist[x + dx] = dist[x] + 1
                q.append(x + dx)


dist[s] = 0
q = deque([(s)])
bfs()
if dist[g] == -1:
    print((-1))
else:
    print((grid.count(".") - dist[g] - 1))

-H, W = list(map(int, input().split())) -map = [[i for i in map(str, sys.stdin.readline().rstrip())] for _ in range(H)] -count_dot = 0 -dist = [[0] * W for _ in range(H)] -for i in range(H): -    for j in range(W): -        if map[i][j] == ".": -            count_dot += 1 +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines +H, W = list(map(int, readline().split())) +grid = ["#"] * (W + 2) +for _ in range(H): +    grid += ["#"] + list(map(str, readline().decode().rstrip())) + ["X"] +grid += ["#"] * (W + 2) +dist = [-1] * ((H + 2) * (W + 2) - 1) +s = W + 3 +g = (H + 1) * (W + 2) - 2 -        x, y = q.popleft() -        if x == H - 1 and y == W - 1: -            break -        dx = [0, 1, 0, -1] -        dy = [1, 0, -1, 0] -        for i in range(4): -            nx = x + dx[i] -            ny = y + dy[i] -            if 0 <= nx < H and 0 <= ny < W and map[nx][ny] == "." and dist[nx][ny] == 0: -                q.append((nx, ny)) -                dist[nx][ny] = dist[x][y] + 1 +        x = q.popleft() +        if x == g: +            return +        for dx in (1, W + 2, -1, -W - 2): +            if dist[x + dx] == -1 and grid[x + dx] == ".": +                dist[x + dx] = dist[x] + 1 +                q.append(x + dx) -q = deque() -q.append((0, 0)) +dist[s] = 0 +q = deque([(s)]) -dist_min = dist[H - 1][W - 1] -if dist_min == 0: +if dist[g] == -1: -    print((count_dot - dist_min - 1)) +    print((grid.count(".") - dist[g] - 1))