### BUGGY CODE ###

from collections import deque
#
# H, W = 10, 37
#
# ARR = [
#     ".....................................",
#     "...#...####...####..###...###...###..",
#     "..#.#..#...#.##....#...#.#...#.#...#.",
#     "..#.#..#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#..##.#.....#...#.#.###.#.###.",
#     ".#####.####..#.....#...#..##....##...",
#     ".#...#.#...#.#.....#...#.#...#.#...#.",
#     ".#...#.#...#.##....#...#.#...#.#...#.",
#     ".#...#.####...####..###...###...###..",
#     ".....................................",
# ]

#

H, W = map(int, input().split())
ARR = []

for i in range(H):
    ARR.append(list(input()))


def calculateDistance(h, w, arr):
    hist = [[0 for j in range(W)] for i in range(H)]

    dist = [[0 for j in range(W)] for i in range(H)]

    dist[0][0] = 1
    q = deque()

    q.append((0, 0))

    while q.__len__() > 0:
        x, y = q.popleft()
        hist[y][x] = 1

        if x == w - 1 and y == h - 1:
            distance = dist[y][x]
            return calculate(h, w,distance, arr)

        # left
        if x > 0 and (hist[y][x - 1] == 0) and (arr[y][x - 1] == "."):
            dist[y][x-1] = dist[y][x] + 1
            q.append((x - 1, y))

        # right
        if x < w - 1 and (hist[y][x + 1] == 0) and (arr[y][x + 1] == "."):
            dist[y][x+1] = dist[y][x] + 1
            q.append((x + 1, y))

        # up
        if y > 0 and (hist[y - 1][x] == 0) and (arr[y - 1][x] == "."):
            dist[y-1][x] = dist[y][x] + 1
            q.append((x, y - 1))

        # down
        if y < h - 1 and (hist[y + 1][x] == 0) and (arr[y + 1][x] == "."):
            dist[y+1][x] = dist[y][x] + 1
            q.append((x, y + 1))

    return -1


def calculate(h, w, distance, arr):
    total = h * w
    alreadyPaiting = 0

    for s in arr:
        for ss in s:
            if ss == "#":
                alreadyPaiting += 1

    return total - distance - alreadyPaiting


res = calculateDistance(H, W, ARR)

print(res)


### FIXED CODE ###

from collections import deque

H, W = 10, 37
ARR = [
    ".....................................",
    "...#...####...####..###...###...###..",
    "..#.#..#...#.##....#...#.#...#.#...#.",
    "..#.#..#...#.#.....#...#.#...#.#...#.",
    ".#...#.#..##.#.....#...#.#.###.#.###.",
    ".#####.####..#.....#...#..##....##...",
    ".#...#.#...#.#.....#...#.#...#.#...#.",
    ".#...#.#...#.##....#...#.#...#.#...#.",
    ".#...#.####...####..###...###...###..",
    ".....................................",
]

H, W = map(int, input().split())
ARR = []

for i in range(H):
    ARR.append(input())


def calculate(h, w, arr):
    q = deque()

    q.append((0, 0))

    dx = [0, 1, 0, -1]
    dy = [-1, 0, 1, 0]

    distance = [[0 for i in range(w)] for j in range(h)]
    distance[0][0] = 1
    while q.__len__() > 0:
        y, x = q.popleft()
        for i in range(4):
            my = y + dy[i]
            mx = x + dx[i]
            if (my >= 0) and (mx >= 0) and (my <= (h - 1)) and (mx <= (w - 1)) and (distance[my][mx] == 0) and (
                    arr[my][mx] == "."):
                distance[my][mx] = distance[y][x] + 1
                q.append((my, mx))

    mmk = 0
    for j in range(h):
        mmk += arr[j].count("#")

    if distance[h - 1][w - 1] == 0:
        print(-1)
    else:
        print(h * w - distance[h - 1][w - 1] - mmk)


calculate(H, W, ARR)