import queue



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

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

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

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



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

que = queue.Queue()

que.put((0, 0))

dist[0][0] = 0



while not que.empty():

  y, x = que.get()

  for i in range(4):

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

    if nx < 0 or nx >= W or ny < 0 or ny >= H:

      continue

    if field[ny][nx] == '#':

      continue

    if dist[ny][nx] != -1:

      continue

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

    que.put((ny, nx))

    

count = sum([f.count('#') for f in field])

      

if dist[H - 1][W - 1] != -1:

  print((H * W - count - dist[H - 1][W - 1] - 1))

else:

  print((-1))

from collections import deque



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

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

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

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



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

que = deque()

que.append((0, 0))

dist[0][0] = 0



while len(que) > 0:

  y, x = que.popleft()

  for i in range(4):

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

    if nx < 0 or nx >= W or ny < 0 or ny >= H:

      continue

    if field[ny][nx] == '#':

      continue

    if dist[ny][nx] != -1:

      continue

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

    que.append((ny, nx))

    

count = sum([f.count('#') for f in field])

      

if dist[H - 1][W - 1] != -1:

  print((H * W - count - dist[H - 1][W - 1] - 1))

else:

  print((-1))
import queue

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
H, W = list(map(int, input().split()))
field = [eval(input()) for _ in range(H)]
dist = [[-1] * W for _ in range(H)]
que = queue.Queue()
que.put((0, 0))
dist[0][0] = 0
while not que.empty():
    y, x = que.get()
    for i in range(4):
        ny, nx = y + dy[i], x + dx[i]
        if nx < 0 or nx >= W or ny < 0 or ny >= H:
            continue
        if field[ny][nx] == "#":
            continue
        if dist[ny][nx] != -1:
            continue
        dist[ny][nx] = dist[y][x] + 1
        que.put((ny, nx))
count = sum([f.count("#") for f in field])
if dist[H - 1][W - 1] != -1:
    print((H * W - count - dist[H - 1][W - 1] - 1))
else:
    print((-1))

from collections import deque

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
H, W = list(map(int, input().split()))
field = [eval(input()) for _ in range(H)]
dist = [[-1] * W for _ in range(H)]
que = deque()
que.append((0, 0))
dist[0][0] = 0
while len(que) > 0:
    y, x = que.popleft()
    for i in range(4):
        ny, nx = y + dy[i], x + dx[i]
        if nx < 0 or nx >= W or ny < 0 or ny >= H:
            continue
        if field[ny][nx] == "#":
            continue
        if dist[ny][nx] != -1:
            continue
        dist[ny][nx] = dist[y][x] + 1
        que.append((ny, nx))
count = sum([f.count("#") for f in field])
if dist[H - 1][W - 1] != -1:
    print((H * W - count - dist[H - 1][W - 1] - 1))
else:
    print((-1))

-import queue +from collections import deque -que = queue.Queue() -que.put((0, 0)) +que = deque() +que.append((0, 0)) -while not que.empty(): -    y, x = que.get() +while len(que) > 0: +    y, x = que.popleft() -        que.put((ny, nx)) +        que.append((ny, nx))