from collections import deque

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

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

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





sy,sx = 0,0

gy,gx = h-1, w-1

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



flg = False

ret = -1

while que:

    y,x,c = que.popleft()

    if y==gy and x==gx:

        flg = True

        ret = c

        break

    if cost[y][x] != -1:

        continue

    cost[y][x] = c

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

        ny = y + dy

        nx = x + dx

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

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

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

if not flg:

    print((-1))

else:

    blk, wht = 0, 0

    for s in S:

        for ss in s:

            if ss=='#':

                blk += 1

            else:

                wht += 1

    print((h*w-ret-1-blk))
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()))
S = [list(eval(input())) for _ in range(h)]
cost = [[-1] * w for _ in range(h)]
sy, sx = 0, 0
gy, gx = h - 1, w - 1
que = deque([(sy, sx, 0)])
flg = False
ret = -1
while que:
    y, x, c = que.popleft()
    if y == gy and x == gx:
        flg = True
        ret = c
        break
    if cost[y][x] != -1:
        continue
    cost[y][x] = c
    for dy, dx in zip((-1, 1, 0, 0), (0, 0, -1, 1)):
        ny = y + dy
        nx = x + dx
        if 0 <= ny < h and 0 <= nx < w:
            if S[ny][nx] != "#":
                que.append((ny, nx, c + 1))
if not flg:
    print((-1))
else:
    blk, wht = 0, 0
    for s in S:
        for ss in s:
            if ss == "#":
                blk += 1
            else:
                wht += 1
    print((h * w - ret - 1 - blk))

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 +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 -h, w = list(map(int, input().split())) -S = [list(eval(input())) for _ in range(h)] -cost = [[-1] * w for _ in range(h)] -sy, sx = 0, 0 -gy, gx = h - 1, w - 1 -que = deque([(sy, sx, 0)]) -flg = False -ret = -1 -while que: -    y, x, c = que.popleft() -    if y == gy and x == gx: -        flg = True -        ret = c -        break -    if cost[y][x] != -1: -        continue -    cost[y][x] = c -    for dy, dx in zip((-1, 1, 0, 0), (0, 0, -1, 1)): -        ny = y + dy -        nx = x + dx -        if 0 <= ny < h and 0 <= nx < w: -            if S[ny][nx] != "#": -                que.append((ny, nx, c + 1)) -if not flg: -    print((-1)) -else: -    blk, wht = 0, 0 -    for s in S: -        for ss in s: -            if ss == "#": -                blk += 1 -            else: -                wht += 1 -    print((h * w - ret - 1 - blk)) + +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))