### BUGGY CODE ###

from collections import deque

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


#

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

for i in range(H):
    l = input()
    ALREADYPAITING += l.count("#")
    ARR.append(l)


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

    q = deque()

    q.append((0, 0, 0, hist))

    distances = []

    while q.__len__() > 0:
        x, y, currentStep, hist = q.popleft()

        if x == w - 1 and y == h - 1:
            distances.append(currentStep + 1)

        hist[y][x] = 1

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

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

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

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

    if distances.__len__() == 0:
        return -1
    else:
        return calculate(h, w, min(distances))


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

    # 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)