from collections import deque

"""

-1 -> not visited

else -> step count

"""

def bfs(maze, visited, sh, sw):

    stack = deque([[sh, sw]])

    visited[sh][sw] = 1

    while stack:

        h, w = stack.popleft()

        # ゴールの条件

        if h == H - 1 and w == W - 1:

            break

        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):

            new_h, new_w = h+j, w+k

            if new_h < 0 or new_h >= H or \

               new_w < 0 or new_w >= W:

                continue

            elif maze[new_h][new_w] != "#" and \

                 visited[new_h][new_w] == -1:

                visited[new_h][new_w] = visited[h][w] + 1

                stack.append([new_h, new_w])

    

    return visited[H-1][W-1]



if __name__ == "__main__":

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

    maze = [input() for _ in range(H)]



    sh, sw = 0, 0



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

    

    step_cnt = bfs(maze, visited, sh, sw)



    white_cnt = sum([i.count(".") for i in maze])



    if step_cnt > 0:

        print(white_cnt - step_cnt)

    else:

        print(-1)

from collections import deque



def dfs(sx, sy):

  stack = deque()

  stack.append([sx, sy])

  visited[sy][sx] = 1

  while stack:

    x, y = stack.popleft()

    if x == w - 1 and y == h - 1:

      break

    for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:

      if 0 <= x + dx < w and 0 <= y + dy < h:

        if S[y + dy][x + dx] != "#" and visited[y + dy][x + dx] == -1:

          visited[y + dy][x + dx] = visited[y][x] + 1

          stack.append([x + dx, y + dy])



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

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



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



dfs(0, 0)



whites = sum([s.count(".") for s in S])



if visited[h - 1][w - 1] != -1:

  ans = whites - visited[h - 1][w - 1]

else:

  ans = -1

  

print(ans)
from collections import deque

"""
-1 -> not visited
else -> step count
"""


def bfs(maze, visited, sh, sw):
    stack = deque([[sh, sw]])
    visited[sh][sw] = 1
    while stack:
        h, w = stack.popleft()
        # ゴールの条件
        if h == H - 1 and w == W - 1:
            break
        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_h, new_w = h + j, w + k
            if new_h < 0 or new_h >= H or new_w < 0 or new_w >= W:
                continue
            elif maze[new_h][new_w] != "#" and visited[new_h][new_w] == -1:
                visited[new_h][new_w] = visited[h][w] + 1
                stack.append([new_h, new_w])
    return visited[H - 1][W - 1]


if __name__ == "__main__":
    H, W = map(int, input().split())
    maze = [input() for _ in range(H)]
    sh, sw = 0, 0
    visited = [[-1] * W for _ in range(H)]
    step_cnt = bfs(maze, visited, sh, sw)
    white_cnt = sum([i.count(".") for i in maze])
    if step_cnt > 0:
        print(white_cnt - step_cnt)
    else:
        print(-1)

from collections import deque


def dfs(sx, sy):
    stack = deque()
    stack.append([sx, sy])
    visited[sy][sx] = 1
    while stack:
        x, y = stack.popleft()
        if x == w - 1 and y == h - 1:
            break
        for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            if 0 <= x + dx < w and 0 <= y + dy < h:
                if S[y + dy][x + dx] != "#" and visited[y + dy][x + dx] == -1:
                    visited[y + dy][x + dx] = visited[y][x] + 1
                    stack.append([x + dx, y + dy])


h, w = list(map(int, input().split()))
S = [eval(input()) for _ in range(h)]
visited = [[-1] * w for _ in range(h)]
dfs(0, 0)
whites = sum([s.count(".") for s in S])
if visited[h - 1][w - 1] != -1:
    ans = whites - visited[h - 1][w - 1]
else:
    ans = -1
print(ans)

-""" --1 -> not visited -else -> step count -""" + +def dfs(sx, sy): +    stack = deque() +    stack.append([sx, sy]) +    visited[sy][sx] = 1 +    while stack: +        x, y = stack.popleft() +        if x == w - 1 and y == h - 1: +            break +        for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: +            if 0 <= x + dx < w and 0 <= y + dy < h: +                if S[y + dy][x + dx] != "#" and visited[y + dy][x + dx] == -1: +                    visited[y + dy][x + dx] = visited[y][x] + 1 +                    stack.append([x + dx, y + dy]) -def bfs(maze, visited, sh, sw): -    stack = deque([[sh, sw]]) -    visited[sh][sw] = 1 -    while stack: -        h, w = stack.popleft() -        # ゴールの条件 -        if h == H - 1 and w == W - 1: -            break -        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]): -            new_h, new_w = h + j, w + k -            if new_h < 0 or new_h >= H or new_w < 0 or new_w >= W: -                continue -            elif maze[new_h][new_w] != "#" and visited[new_h][new_w] == -1: -                visited[new_h][new_w] = visited[h][w] + 1 -                stack.append([new_h, new_w]) -    return visited[H - 1][W - 1] - - -if __name__ == "__main__": -    H, W = map(int, input().split()) -    maze = [input() for _ in range(H)] -    sh, sw = 0, 0 -    visited = [[-1] * W for _ in range(H)] -    step_cnt = bfs(maze, visited, sh, sw) -    white_cnt = sum([i.count(".") for i in maze]) -    if step_cnt > 0: -        print(white_cnt - step_cnt) -    else: -        print(-1) +h, w = list(map(int, input().split())) +S = [eval(input()) for _ in range(h)] +visited = [[-1] * w for _ in range(h)] +dfs(0, 0) +whites = sum([s.count(".") for s in S]) +if visited[h - 1][w - 1] != -1: +    ans = whites - visited[h - 1][w - 1] +else: +    ans = -1 +print(ans)