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


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



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

    q = deque()

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

    distances = []

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

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

            if currentStep + 1 <= h + w:
                break

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

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

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

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

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


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)