from collections import deque

def bfs():

  d=[[float("inf")]*m for _ in range(n)]

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

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

  

  que=deque([])

  que.append((0,0))

  d[0][0]=0



  while que:

    p = que.popleft()

    if p[0]==n-1 and p[1]==m-1:

      break

    for i in range(4):

      nx=p[0]+dx[i]

      ny=p[1]+dy[i]

      if 0 <= nx < n and 0 <= ny < m and c[nx][ny] != "#" and d[nx][ny] == float("inf"):

                # 移動できるならキューに入れ、その点の距離を p からの距離 +1 で確定する

                que.append((nx, ny))

                d[nx][ny] = d[p[0]][p[1]] + 1

  return d[n-1][m-1]



n, m = list(map(int, input().split()))

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



count=0

for i in range(n):

  for j in range(m):

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

      count+=1

g=bfs()

if 0<g<float("inf"):

  print((n*m-g-1-count))

else:

  print((-1))
from collections import deque

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

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

dhdw = [[-1,0],[1,0],[0,-1],[0,1]]

min_dis=[[-1]*w for _ in range(h)]

que=deque()

def bfs(x,y):

  que.append([x,y])

  min_dis[x][y]=0

  while que:

    sh,sw=que.popleft()

    for dh,dw in dhdw:

      nh,nw = sh+dh,sw+dw

      if not(0<=nh<=h-1) or not(0<=nw<=w-1) or min_dis[nh][nw]!=-1 or c[nh][nw]=='#':

        continue

      min_dis[nh][nw]=min_dis[sh][sw]+1

      que.append([nh,nw])

      if [nh,nw]==[h-1,w-1]:

        return min_dis[sh][sw]+1

a=bfs(0,0) or 0

if a==0:

  print((-1))

  exit()

ans=0

for i in c:

  ans+=i.count(".")

print((ans-1-a))
from collections import deque


def bfs():
    d = [[float("inf")] * m for _ in range(n)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    que = deque([])
    que.append((0, 0))
    d[0][0] = 0
    while que:
        p = que.popleft()
        if p[0] == n - 1 and p[1] == m - 1:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (
                0 <= nx < n
                and 0 <= ny < m
                and c[nx][ny] != "#"
                and d[nx][ny] == float("inf")
            ):
                # 移動できるならキューに入れ、その点の距離を p からの距離 +1 で確定する
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
    return d[n - 1][m - 1]


n, m = list(map(int, input().split()))
c = [list(eval(input())) for i in range(n)]
count = 0
for i in range(n):
    for j in range(m):
        if c[i][j] == "#":
            count += 1
g = bfs()
if 0 < g < float("inf"):
    print((n * m - g - 1 - count))
else:
    print((-1))

from collections import deque

h, w = list(map(int, input().split()))
c = [list(eval(input())) for _ in range(h)]
dhdw = [[-1, 0], [1, 0], [0, -1], [0, 1]]
min_dis = [[-1] * w for _ in range(h)]
que = deque()


def bfs(x, y):
    que.append([x, y])
    min_dis[x][y] = 0
    while que:
        sh, sw = que.popleft()
        for dh, dw in dhdw:
            nh, nw = sh + dh, sw + dw
            if (
                not (0 <= nh <= h - 1)
                or not (0 <= nw <= w - 1)
                or min_dis[nh][nw] != -1
                or c[nh][nw] == "#"
            ):
                continue
            min_dis[nh][nw] = min_dis[sh][sw] + 1
            que.append([nh, nw])
            if [nh, nw] == [h - 1, w - 1]:
                return min_dis[sh][sw] + 1


a = bfs(0, 0) or 0
if a == 0:
    print((-1))
    exit()
ans = 0
for i in c:
    ans += i.count(".")
print((ans - 1 - a))

- -def bfs(): -    d = [[float("inf")] * m for _ in range(n)] -    dx = [1, 0, -1, 0] -    dy = [0, 1, 0, -1] -    que = deque([]) -    que.append((0, 0)) -    d[0][0] = 0 -    while que: -        p = que.popleft() -        if p[0] == n - 1 and p[1] == m - 1: -            break -        for i in range(4): -            nx = p[0] + dx[i] -            ny = p[1] + dy[i] -            if ( -                0 <= nx < n -                and 0 <= ny < m -                and c[nx][ny] != "#" -                and d[nx][ny] == float("inf") -            ): -                # 移動できるならキューに入れ、その点の距離を p からの距離 +1 で確定する -                que.append((nx, ny)) -                d[nx][ny] = d[p[0]][p[1]] + 1 -    return d[n - 1][m - 1] +h, w = list(map(int, input().split())) +c = [list(eval(input())) for _ in range(h)] +dhdw = [[-1, 0], [1, 0], [0, -1], [0, 1]] +min_dis = [[-1] * w for _ in range(h)] +que = deque() -n, m = list(map(int, input().split())) -c = [list(eval(input())) for i in range(n)] -count = 0 -for i in range(n): -    for j in range(m): -        if c[i][j] == "#": -            count += 1 -g = bfs() -if 0 < g < float("inf"): -    print((n * m - g - 1 - count)) -else: +def bfs(x, y): +    que.append([x, y]) +    min_dis[x][y] = 0 +    while que: +        sh, sw = que.popleft() +        for dh, dw in dhdw: +            nh, nw = sh + dh, sw + dw +            if ( +                not (0 <= nh <= h - 1) +                or not (0 <= nw <= w - 1) +                or min_dis[nh][nw] != -1 +                or c[nh][nw] == "#" +            ): +                continue +            min_dis[nh][nw] = min_dis[sh][sw] + 1 +            que.append([nh, nw]) +            if [nh, nw] == [h - 1, w - 1]: +                return min_dis[sh][sw] + 1 + + +a = bfs(0, 0) or 0 +if a == 0: +    exit() +ans = 0 +for i in c: +    ans += i.count(".") +print((ans - 1 - a))