R,C = list(map(int,input().split()))

sy,sx = 1,1

gy,gx = R,C

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

mx = 0

for m in maze:

  mx += m.count('.')





sx,sy = sx-1, sy-1

gx,gy = gx-1, gy-1



def bfs(R,C,sy,sx,gy,gx,maze):

    from collections import deque

    import numpy as np

    INF = np.inf

    visited = [[INF]*C for _ in range(R)]

    visited[sy][sx] = 0

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

    while que:

        y,x = que.popleft()

        for i,j in ((1,0),(0,1),(-1,0),(0,-1)):

            ny,nx = y+i, x+j

            if 0<=ny<R and 0<=nx<C:

                if (maze[ny][nx]!='#') and visited[ny][nx]==INF:

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

                    que.append((ny,nx))

    if visited[gy][gx]!=INF:

      return visited[gy][gx]

    else:

      return -1



ans = bfs(R,C,sy,sx,gy,gx,maze)

print((mx-ans-1 if ans!=-1 else -1))
from collections import deque

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

maze = []

visited = [[0]*(w) for _ in range(h)]

mx = 0

for _ in range(h):

    s = eval(input())

    mx += s.count('.')

    maze.append(list(s))

sy,sx = 0, 0

gy, gx = h-1, w-1

que = deque([(sy,sx,1)])



while que:

    y,x,c = que.popleft()

    if visited[y][x] != 0:

        continue

    visited[y][x] = c

    for dy,dx in zip((1,-1,0,0), (0,0,1,-1)):

        ny,nx = y+dy, x+dx

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

            if maze[ny][nx]=='.':

                que.append((ny,nx,c+1))



print((mx-visited[gy][gx] if visited[gy][gx]!=0 else -1))

                
R, C = list(map(int, input().split()))
sy, sx = 1, 1
gy, gx = R, C
maze = [list(eval(input())) for _ in range(R)]
mx = 0
for m in maze:
    mx += m.count(".")
sx, sy = sx - 1, sy - 1
gx, gy = gx - 1, gy - 1


def bfs(R, C, sy, sx, gy, gx, maze):
    from collections import deque
    import numpy as np

    INF = np.inf
    visited = [[INF] * C for _ in range(R)]
    visited[sy][sx] = 0
    que = deque([(sy, sx)])
    while que:
        y, x = que.popleft()
        for i, j in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            ny, nx = y + i, x + j
            if 0 <= ny < R and 0 <= nx < C:
                if (maze[ny][nx] != "#") and visited[ny][nx] == INF:
                    visited[ny][nx] = visited[y][x] + 1
                    que.append((ny, nx))
    if visited[gy][gx] != INF:
        return visited[gy][gx]
    else:
        return -1


ans = bfs(R, C, sy, sx, gy, gx, maze)
print((mx - ans - 1 if ans != -1 else -1))

from collections import deque

h, w = list(map(int, input().split()))
maze = []
visited = [[0] * (w) for _ in range(h)]
mx = 0
for _ in range(h):
    s = eval(input())
    mx += s.count(".")
    maze.append(list(s))
sy, sx = 0, 0
gy, gx = h - 1, w - 1
que = deque([(sy, sx, 1)])
while que:
    y, x, c = que.popleft()
    if visited[y][x] != 0:
        continue
    visited[y][x] = c
    for dy, dx in zip((1, -1, 0, 0), (0, 0, 1, -1)):
        ny, nx = y + dy, x + dx
        if 0 <= ny < h and 0 <= nx < w:
            if maze[ny][nx] == ".":
                que.append((ny, nx, c + 1))
print((mx - visited[gy][gx] if visited[gy][gx] != 0 else -1))

-R, C = list(map(int, input().split())) -sy, sx = 1, 1 -gy, gx = R, C -maze = [list(eval(input())) for _ in range(R)] +from collections import deque + +h, w = list(map(int, input().split())) +maze = [] +visited = [[0] * (w) for _ in range(h)] -for m in maze: -    mx += m.count(".") -sx, sy = sx - 1, sy - 1 -gx, gy = gx - 1, gy - 1 - - -def bfs(R, C, sy, sx, gy, gx, maze): -    from collections import deque -    import numpy as np - -    INF = np.inf -    visited = [[INF] * C for _ in range(R)] -    visited[sy][sx] = 0 -    que = deque([(sy, sx)]) -    while que: -        y, x = que.popleft() -        for i, j in ((1, 0), (0, 1), (-1, 0), (0, -1)): -            ny, nx = y + i, x + j -            if 0 <= ny < R and 0 <= nx < C: -                if (maze[ny][nx] != "#") and visited[ny][nx] == INF: -                    visited[ny][nx] = visited[y][x] + 1 -                    que.append((ny, nx)) -    if visited[gy][gx] != INF: -        return visited[gy][gx] -    else: -        return -1 - - -ans = bfs(R, C, sy, sx, gy, gx, maze) -print((mx - ans - 1 if ans != -1 else -1)) +for _ in range(h): +    s = eval(input()) +    mx += s.count(".") +    maze.append(list(s)) +sy, sx = 0, 0 +gy, gx = h - 1, w - 1 +que = deque([(sy, sx, 1)]) +while que: +    y, x, c = que.popleft() +    if visited[y][x] != 0: +        continue +    visited[y][x] = c +    for dy, dx in zip((1, -1, 0, 0), (0, 0, 1, -1)): +        ny, nx = y + dy, x + dx +        if 0 <= ny < h and 0 <= nx < w: +            if maze[ny][nx] == ".": +                que.append((ny, nx, c + 1)) +print((mx - visited[gy][gx] if visited[gy][gx] != 0 else -1))