from collections import deque





def resolve():

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

    maze = []

    cnt = 0

    for i in range(H):

        maze.append(list(eval(input())))

        cnt += maze[-1].count('#')

    

    d = [[float('inf')] * W for _ in range(H)]

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

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

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

    d[0][0] = 0

    goal = 0



    while q:

        y, x = q.popleft()

        for i in range(4):

            ny = y + dy[i]

            nx = x + dx[i]

            if (1

                    and 0 <= ny < H

                    and 0 <= nx < W

                    and maze[ny][nx] != '#'

                    and d[ny][nx] == float('inf')):

                q.append((ny, nx))

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

        if d[H - 1][W - 1] != float('inf'):

            goal = d[H - 1][W - 1] + 1

            break

    if goal:

        print((H * W - goal - cnt))

    else:

        print((-1))



if __name__ == "__main__":

    resolve()

#!/usr/bin/env python3

def main():

    from collections import deque



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

    S = []

    num_path = 0

    for _ in range(H):

        s = eval(input())

        num_path += s.count('.')

        S.append(s)



    move = ((0, 1), (0, -1), (1, 0), (-1, 0))

    distance = [[1] * W for _ in range(H)]

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

    while queue:

        y, x = queue.popleft()

        if y == H - 1 and x == W - 1:

            break

        for dy, dx in move:

            ny = y + dy

            nx = x + dx

            if (1

                    and 0 <= ny < H

                    and 0 <= nx < W

                    and distance[ny][nx] == 1

                    and S[ny][nx] == '.'):

                queue.append((ny, nx))

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

    if distance[H - 1][W - 1] == 1:

        print((-1))

    else:

        print((num_path - distance[H - 1][W - 1]))





if __name__ == '__main__':

    main()

from collections import deque


def resolve():
    H, W = list(map(int, input().split()))
    maze = []
    cnt = 0
    for i in range(H):
        maze.append(list(eval(input())))
        cnt += maze[-1].count("#")
    d = [[float("inf")] * W for _ in range(H)]
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    q = deque([(0, 0)])
    d[0][0] = 0
    goal = 0
    while q:
        y, x = q.popleft()
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if (
                1
                and 0 <= ny < H
                and 0 <= nx < W
                and maze[ny][nx] != "#"
                and d[ny][nx] == float("inf")
            ):
                q.append((ny, nx))
                d[ny][nx] = d[y][x] + 1
        if d[H - 1][W - 1] != float("inf"):
            goal = d[H - 1][W - 1] + 1
            break
    if goal:
        print((H * W - goal - cnt))
    else:
        print((-1))


if __name__ == "__main__":
    resolve()

#!/usr/bin/env python3
def main():
    from collections import deque

    H, W = list(map(int, input().split()))
    S = []
    num_path = 0
    for _ in range(H):
        s = eval(input())
        num_path += s.count(".")
        S.append(s)
    move = ((0, 1), (0, -1), (1, 0), (-1, 0))
    distance = [[1] * W for _ in range(H)]
    queue = deque([(0, 0)])
    while queue:
        y, x = queue.popleft()
        if y == H - 1 and x == W - 1:
            break
        for dy, dx in move:
            ny = y + dy
            nx = x + dx
            if (
                1
                and 0 <= ny < H
                and 0 <= nx < W
                and distance[ny][nx] == 1
                and S[ny][nx] == "."
            ):
                queue.append((ny, nx))
                distance[ny][nx] = distance[y][x] + 1
    if distance[H - 1][W - 1] == 1:
        print((-1))
    else:
        print((num_path - distance[H - 1][W - 1]))


if __name__ == "__main__":
    main()

-from collections import deque +#!/usr/bin/env python3 +def main(): +    from collections import deque - -def resolve(): -    maze = [] -    cnt = 0 -    for i in range(H): -        maze.append(list(eval(input()))) -        cnt += maze[-1].count("#") -    d = [[float("inf")] * W for _ in range(H)] -    dy = [1, 0, -1, 0] -    dx = [0, 1, 0, -1] -    q = deque([(0, 0)]) -    d[0][0] = 0 -    goal = 0 -    while q: -        y, x = q.popleft() -        for i in range(4): -            ny = y + dy[i] -            nx = x + dx[i] +    S = [] +    num_path = 0 +    for _ in range(H): +        s = eval(input()) +        num_path += s.count(".") +        S.append(s) +    move = ((0, 1), (0, -1), (1, 0), (-1, 0)) +    distance = [[1] * W for _ in range(H)] +    queue = deque([(0, 0)]) +    while queue: +        y, x = queue.popleft() +        if y == H - 1 and x == W - 1: +            break +        for dy, dx in move: +            ny = y + dy +            nx = x + dx -                and maze[ny][nx] != "#" -                and d[ny][nx] == float("inf") +                and distance[ny][nx] == 1 +                and S[ny][nx] == "." -                q.append((ny, nx)) -                d[ny][nx] = d[y][x] + 1 -        if d[H - 1][W - 1] != float("inf"): -            goal = d[H - 1][W - 1] + 1 -            break -    if goal: -        print((H * W - goal - cnt)) +                queue.append((ny, nx)) +                distance[ny][nx] = distance[y][x] + 1 +    if distance[H - 1][W - 1] == 1: +        print((-1)) -        print((-1)) +        print((num_path - distance[H - 1][W - 1])) -    resolve() +    main()