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

c = [eval(input()) for _ in range(H)]



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



def bfs():

    visited = [[False for _ in range(W)] for _ in range(H)]

    dist = [[[] for _ in range(W)] for _ in range(H)]

    routes = [(0, 0)]

    visited[0][0] = True



    hop_count = 0

    while len(routes) > 0:

        x, y = routes.pop(0)

        for move in moves:

            cx, cy = x + move[0], y + move[1]

            if cx < 0 or cy < 0 or cx >= W or cy >= H or c[cy][cx] == '#' or visited[cy][cx]:

                continue

            if cx == W-1 and cy == H-1:

                return sum([line.count('.')for line in c]) - len(dist[y][x]) - 2

            visited[cy][cx] = True

            routes.append((cx, cy))

            dist[cy][cx] += dist[y][x] + [(x, y)]

    return -1



print((bfs()))
H, W = list(map(int, input().split()))

c = [eval(input()) for _ in range(H)]



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



def bfs():

    visited = [[False for _ in range(W)] for _ in range(H)]

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

    routes = [(0, 0)]

    visited[0][0] = True



    while len(routes) > 0:

        x, y = routes.pop(0)

        for move in moves:

            cx, cy = x + move[0], y + move[1]

            if cx < 0 or cy < 0 or cx >= W or cy >= H or c[cy][cx] == '#' or visited[cy][cx]:

                continue

            if cx == W-1 and cy == H-1:

                return sum([line.count('.')for line in c]) - dist[y][x] - 2

            visited[cy][cx] = True

            routes.append((cx, cy))

            dist[cy][cx] = dist[y][x] + 1

    return -1



print((bfs()))
H, W = list(map(int, input().split()))
c = [eval(input()) for _ in range(H)]
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]


def bfs():
    visited = [[False for _ in range(W)] for _ in range(H)]
    dist = [[[] for _ in range(W)] for _ in range(H)]
    routes = [(0, 0)]
    visited[0][0] = True
    hop_count = 0
    while len(routes) > 0:
        x, y = routes.pop(0)
        for move in moves:
            cx, cy = x + move[0], y + move[1]
            if (
                cx < 0
                or cy < 0
                or cx >= W
                or cy >= H
                or c[cy][cx] == "#"
                or visited[cy][cx]
            ):
                continue
            if cx == W - 1 and cy == H - 1:
                return sum([line.count(".") for line in c]) - len(dist[y][x]) - 2
            visited[cy][cx] = True
            routes.append((cx, cy))
            dist[cy][cx] += dist[y][x] + [(x, y)]
    return -1


print((bfs()))

H, W = list(map(int, input().split()))
c = [eval(input()) for _ in range(H)]
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]


def bfs():
    visited = [[False for _ in range(W)] for _ in range(H)]
    dist = [[0 for _ in range(W)] for _ in range(H)]
    routes = [(0, 0)]
    visited[0][0] = True
    while len(routes) > 0:
        x, y = routes.pop(0)
        for move in moves:
            cx, cy = x + move[0], y + move[1]
            if (
                cx < 0
                or cy < 0
                or cx >= W
                or cy >= H
                or c[cy][cx] == "#"
                or visited[cy][cx]
            ):
                continue
            if cx == W - 1 and cy == H - 1:
                return sum([line.count(".") for line in c]) - dist[y][x] - 2
            visited[cy][cx] = True
            routes.append((cx, cy))
            dist[cy][cx] = dist[y][x] + 1
    return -1


print((bfs()))

-    dist = [[[] for _ in range(W)] for _ in range(H)] +    dist = [[0 for _ in range(W)] for _ in range(H)] -    hop_count = 0 -                return sum([line.count(".") for line in c]) - len(dist[y][x]) - 2 +                return sum([line.count(".") for line in c]) - dist[y][x] - 2 -            dist[cy][cx] += dist[y][x] + [(x, y)] +            dist[cy][cx] = dist[y][x] + 1