# ABC088D - Grid Repainting

from collections import deque



def bfs() -> None:

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

    q = deque([(0, 0)])

    while q:

        x, y = q.popleft()

        for dx, dy in dxy:

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

            # find unsearched grids of S and keep them in the queue

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

                q.append((nx, ny))

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





def main():

    # shortest path needs to remain white, the rest can be black

    global H, W, S, D

    H, W, *S = open(0).read().split()

    H, W = int(H), int(W)

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

    D[0][0] = 1

    bfs()

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

    ans = white - x if x else -1  # x = 0 <-> can't make a path to goal

    print(ans)





if __name__ == "__main__":

    main()
# ABC088D - Grid Repainting

def bfs() -> None:

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

    q = [(0, 0)]

    while q:

        x, y = q.pop(0)

        for dx, dy in dxy:

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

            # find unsearched grids of S and keep them in the queue

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

                q.append((nx, ny))

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





def main():

    # shortest path needs to remain white, the rest can be black

    global H, W, S, D

    H, W, *S = open(0).read().split()

    H, W = int(H), int(W)

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

    D[0][0] = 1

    bfs()

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

    ans = white - x if x else -1  # x = 0 <-> can't make a path to goal

    print(ans)





if __name__ == "__main__":

    main()
# ABC088D - Grid Repainting
from collections import deque


def bfs() -> None:
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    q = deque([(0, 0)])
    while q:
        x, y = q.popleft()
        for dx, dy in dxy:
            nx, ny = x + dx, y + dy  # next x, y
            # find unsearched grids of S and keep them in the queue
            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and D[nx][ny] == 0:
                q.append((nx, ny))
                D[nx][ny] = D[x][y] + 1


def main():
    # shortest path needs to remain white, the rest can be black
    global H, W, S, D
    H, W, *S = open(0).read().split()
    H, W = int(H), int(W)
    D = [[0] * W for _ in range(H)]  # distance from (0, 0) to (x, y)
    D[0][0] = 1
    bfs()
    white, x = sum(s.count(".") for s in S), D[-1][-1]
    ans = white - x if x else -1  # x = 0 <-> can't make a path to goal
    print(ans)


if __name__ == "__main__":
    main()

# ABC088D - Grid Repainting
def bfs() -> None:
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    q = [(0, 0)]
    while q:
        x, y = q.pop(0)
        for dx, dy in dxy:
            nx, ny = x + dx, y + dy  # next x, y
            # find unsearched grids of S and keep them in the queue
            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "." and D[nx][ny] == 0:
                q.append((nx, ny))
                D[nx][ny] = D[x][y] + 1


def main():
    # shortest path needs to remain white, the rest can be black
    global H, W, S, D
    H, W, *S = open(0).read().split()
    H, W = int(H), int(W)
    D = [[0] * W for _ in range(H)]  # distance from (0, 0) to (x, y)
    D[0][0] = 1
    bfs()
    white, x = sum(s.count(".") for s in S), D[-1][-1]
    ans = white - x if x else -1  # x = 0 <-> can't make a path to goal
    print(ans)


if __name__ == "__main__":
    main()

-from collections import deque - - -    q = deque([(0, 0)]) +    q = [(0, 0)] -        x, y = q.popleft() +        x, y = q.pop(0)