# ABC088D - Grid Repainting

import sys

input = sys.stdin.readline



from collections import deque



def bfs() -> None:

    q = [(0, 0)]

    while q:

        x, y = q.pop(0)

        for dx, dy in dxy:

            nx, ny = x + dx, y + dy  # next x, y

            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and dist[nx][ny] == 0:

                q.append((nx, ny))

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





def main():

    global H, W, S, dist, dxy

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

    S = tuple(input().rstrip() for _ in range(H))

    dist = [[0] * W for _ in range(H)]  # distance from (0, 0) to (x, y)

    dist[0][0] = 1

    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]

    bfs()

    white, x = sum(s.count(".") for s in S), dist[-1][-1]

    ans = white - x if x else -1

    print(ans)





if __name__ == "__main__":

    main()
# ABC088D - Grid Repainting

import sys

input = sys.stdin.readline



def bfs() -> None:

    q = [(0, 0)]

    while q:

        x, y = q.pop(0)

        for dx, dy in dxy:

            nx, ny = x + dx, y + dy  # next x, y

            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and dist[nx][ny] == 0:

                q.append((nx, ny))

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





def main():

    global H, W, S, dist, dxy

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

    S = tuple(input().rstrip() for _ in range(H))

    dist = [[0] * W for _ in range(H)]  # distance from (0, 0) to (x, y)

    dist[0][0] = 1

    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]

    bfs()

    white, x = sum(s.count(".") for s in S), dist[-1][-1]

    ans = white - x if x else -1

    print(ans)





if __name__ == "__main__":

    main()
# ABC088D - Grid Repainting
import sys

input = sys.stdin.readline
from collections import deque


def bfs() -> None:
    q = [(0, 0)]
    while q:
        x, y = q.pop(0)
        for dx, dy in dxy:
            nx, ny = x + dx, y + dy  # next x, y
            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and dist[nx][ny] == 0:
                q.append((nx, ny))
                dist[nx][ny] = dist[x][y] + 1


def main():
    global H, W, S, dist, dxy
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    dist = [[0] * W for _ in range(H)]  # distance from (0, 0) to (x, y)
    dist[0][0] = 1
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    bfs()
    white, x = sum(s.count(".") for s in S), dist[-1][-1]
    ans = white - x if x else -1
    print(ans)


if __name__ == "__main__":
    main()

# ABC088D - Grid Repainting
import sys

input = sys.stdin.readline


def bfs() -> None:
    q = [(0, 0)]
    while q:
        x, y = q.pop(0)
        for dx, dy in dxy:
            nx, ny = x + dx, y + dy  # next x, y
            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and dist[nx][ny] == 0:
                q.append((nx, ny))
                dist[nx][ny] = dist[x][y] + 1


def main():
    global H, W, S, dist, dxy
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    dist = [[0] * W for _ in range(H)]  # distance from (0, 0) to (x, y)
    dist[0][0] = 1
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    bfs()
    white, x = sum(s.count(".") for s in S), dist[-1][-1]
    ans = white - x if x else -1
    print(ans)


if __name__ == "__main__":
    main()

-from collections import deque