from sys import stdin, setrecursionlimit

from collections import deque



setrecursionlimit(10 ** 9)





def input():

    return stdin.readline().strip()





def main():

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

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



    gx, gy = H - 1, W - 1

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

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

    dist[0][0] = 1



    while q:

        x, y = q.popleft()

        if x == gx and y == gy:

            break



        for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):

            nx, ny = x + dx, y + dy

            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] == -1 and s[nx][ny] == '.':

                q.append((nx, ny))

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



    if dist[gx][gy] == -1:

        print((-1))

    else:

        ans = sum(row.count('.') for row in s) - dist[gx][gy]

        print(ans)



if __name__ == '__main__':

    main()

import sys

from collections import deque



read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60





def main():

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

    G = [0] * H

    for i in range(H):

        G[i] = readline().strip()

    dxdy4 = ((-1, 0), (0, -1), (0, 1), (1, 0))



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

    dist[0][0] = 1

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

    while queue:

        x, y = queue.popleft()

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

            break

        for dx, dy in dxdy4:

            nx, ny = x + dx, y + dy

            if 0 <= nx < H and 0 <= ny < W and G[nx][ny] == '.' and dist[nx][ny] == -1:

                queue.append((nx, ny))

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



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

        print((-1))

    else:

        blacks = sum(row.count('#') for row in G)

        print((H * W - dist[H - 1][W - 1] - blacks))



    return





if __name__ == '__main__':

    main()

from sys import stdin, setrecursionlimit
from collections import deque

setrecursionlimit(10**9)


def input():
    return stdin.readline().strip()


def main():
    H, W = list(map(int, input().split()))
    s = [eval(input()) for _ in range(H)]
    gx, gy = H - 1, W - 1
    q = deque([(0, 0)])
    dist = [[-1] * W for _ in range(H)]
    dist[0][0] = 1
    while q:
        x, y = q.popleft()
        if x == gx and y == gy:
            break
        for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
            nx, ny = x + dx, y + dy
            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] == -1 and s[nx][ny] == ".":
                q.append((nx, ny))
                dist[nx][ny] = dist[x][y] + 1
    if dist[gx][gy] == -1:
        print((-1))
    else:
        ans = sum(row.count(".") for row in s) - dist[gx][gy]
        print(ans)


if __name__ == "__main__":
    main()

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def main():
    H, W = list(map(int, readline().split()))
    G = [0] * H
    for i in range(H):
        G[i] = readline().strip()
    dxdy4 = ((-1, 0), (0, -1), (0, 1), (1, 0))
    dist = [[-1] * W for _ in range(H)]
    dist[0][0] = 1
    queue = deque([(0, 0)])
    while queue:
        x, y = queue.popleft()
        if x == H - 1 and y == W - 1:
            break
        for dx, dy in dxdy4:
            nx, ny = x + dx, y + dy
            if 0 <= nx < H and 0 <= ny < W and G[nx][ny] == "." and dist[nx][ny] == -1:
                queue.append((nx, ny))
                dist[nx][ny] = dist[x][y] + 1
    if dist[H - 1][W - 1] == -1:
        print((-1))
    else:
        blacks = sum(row.count("#") for row in G)
        print((H * W - dist[H - 1][W - 1] - blacks))
    return


if __name__ == "__main__":
    main()

-from sys import stdin, setrecursionlimit +import sys -setrecursionlimit(10**9) - - -def input(): -    return stdin.readline().strip() +read = sys.stdin.read +readline = sys.stdin.readline +readlines = sys.stdin.readlines +sys.setrecursionlimit(10**9) +INF = 1 << 60 -    H, W = list(map(int, input().split())) -    s = [eval(input()) for _ in range(H)] -    gx, gy = H - 1, W - 1 -    q = deque([(0, 0)]) +    H, W = list(map(int, readline().split())) +    G = [0] * H +    for i in range(H): +        G[i] = readline().strip() +    dxdy4 = ((-1, 0), (0, -1), (0, 1), (1, 0)) -    while q: -        x, y = q.popleft() -        if x == gx and y == gy: +    queue = deque([(0, 0)]) +    while queue: +        x, y = queue.popleft() +        if x == H - 1 and y == W - 1: -        for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)): +        for dx, dy in dxdy4: -            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] == -1 and s[nx][ny] == ".": -                q.append((nx, ny)) +            if 0 <= nx < H and 0 <= ny < W and G[nx][ny] == "." and dist[nx][ny] == -1: +                queue.append((nx, ny)) -    if dist[gx][gy] == -1: +    if dist[H - 1][W - 1] == -1: -        ans = sum(row.count(".") for row in s) - dist[gx][gy] -        print(ans) +        blacks = sum(row.count("#") for row in G) +        print((H * W - dist[H - 1][W - 1] - blacks)) +    return