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

import numpy as np

Grid = np.array([[0 if x == '.' else -1 for x in eval(input())] for _ in range(H)],dtype='int64')



def solveMase(Grid, start, goal): #bfsで迷路を解き、スタート-ゴール間の最低必要白マス数を返す

    seen = {start}

    V = [start]

    while len(V) != 0: #bfs

        v = V.pop(0)

        if v == goal:

            return Grid[goal]

        #移動先nvをリストにまとめる

        y,x = v

        nV = []

        if x > 0: nV.append((y,x-1))

        if y > 0: nV.append((y-1,x))

        if x < W-1: nV.append((y,x+1))

        if y < H-1: nV.append((y+1,x))

        #bfs

        for nv in nV:

            if Grid[nv] == -1 or nv in seen:

                continue

            Grid[nv] = Grid[v] + 1

            V.append(nv)

            seen.add(nv)

    return 0



white = len(Grid[Grid == 0])-1

nes_white = solveMase(Grid, (0,0), (H-1,W-1))

print((white-nes_white if nes_white > 0 else -1))
from collections import deque, Counter



def main():

    with open(0) as f:

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

        maze = [list(line) for line in f.readlines()]



    rank = [[None]*W for _ in range(H)]

    rank[0][0] = 0

    #bfs

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

    while len(reserved) > 0:

        x, y = reserved.popleft()

        for u,v in [(s,t) for s,t in [(x-1,y), (x+1,y), (x,y-1), (x,y+1)] if 0<=s<H and 0<=t<W]:

            if rank[u][v] is not None:

                continue

            if maze[u][v] == '.':

                rank[u][v] = rank[x][y] + 1

                reserved.append((u,v))



    if rank[H-1][W-1] is None:

        print((-1))

    else:

        counter = Counter()

        for line in maze:

            counter.update(line)

        print((counter['.'] - rank[H-1][W-1]-1))



main()
H, W = list(map(int, input().split()))
import numpy as np

Grid = np.array(
    [[0 if x == "." else -1 for x in eval(input())] for _ in range(H)], dtype="int64"
)


def solveMase(Grid, start, goal):  # bfsで迷路を解き、スタート-ゴール間の最低必要白マス数を返す
    seen = {start}
    V = [start]
    while len(V) != 0:  # bfs
        v = V.pop(0)
        if v == goal:
            return Grid[goal]
        # 移動先nvをリストにまとめる
        y, x = v
        nV = []
        if x > 0:
            nV.append((y, x - 1))
        if y > 0:
            nV.append((y - 1, x))
        if x < W - 1:
            nV.append((y, x + 1))
        if y < H - 1:
            nV.append((y + 1, x))
        # bfs
        for nv in nV:
            if Grid[nv] == -1 or nv in seen:
                continue
            Grid[nv] = Grid[v] + 1
            V.append(nv)
            seen.add(nv)
    return 0


white = len(Grid[Grid == 0]) - 1
nes_white = solveMase(Grid, (0, 0), (H - 1, W - 1))
print((white - nes_white if nes_white > 0 else -1))

from collections import deque, Counter


def main():
    with open(0) as f:
        H, W = list(map(int, f.readline().split()))
        maze = [list(line) for line in f.readlines()]
    rank = [[None] * W for _ in range(H)]
    rank[0][0] = 0
    # bfs
    reserved = deque([(0, 0)])
    while len(reserved) > 0:
        x, y = reserved.popleft()
        for u, v in [
            (s, t)
            for s, t in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
            if 0 <= s < H and 0 <= t < W
        ]:
            if rank[u][v] is not None:
                continue
            if maze[u][v] == ".":
                rank[u][v] = rank[x][y] + 1
                reserved.append((u, v))
    if rank[H - 1][W - 1] is None:
        print((-1))
    else:
        counter = Counter()
        for line in maze:
            counter.update(line)
        print((counter["."] - rank[H - 1][W - 1] - 1))


main()

-H, W = list(map(int, input().split())) -import numpy as np - -Grid = np.array( -    [[0 if x == "." else -1 for x in eval(input())] for _ in range(H)], dtype="int64" -) +from collections import deque, Counter -def solveMase(Grid, start, goal):  # bfsで迷路を解き、スタート-ゴール間の最低必要白マス数を返す -    seen = {start} -    V = [start] -    while len(V) != 0:  # bfs -        v = V.pop(0) -        if v == goal: -            return Grid[goal] -        # 移動先nvをリストにまとめる -        y, x = v -        nV = [] -        if x > 0: -            nV.append((y, x - 1)) -        if y > 0: -            nV.append((y - 1, x)) -        if x < W - 1: -            nV.append((y, x + 1)) -        if y < H - 1: -            nV.append((y + 1, x)) -        # bfs -        for nv in nV: -            if Grid[nv] == -1 or nv in seen: +def main(): +    with open(0) as f: +        H, W = list(map(int, f.readline().split())) +        maze = [list(line) for line in f.readlines()] +    rank = [[None] * W for _ in range(H)] +    rank[0][0] = 0 +    # bfs +    reserved = deque([(0, 0)]) +    while len(reserved) > 0: +        x, y = reserved.popleft() +        for u, v in [ +            (s, t) +            for s, t in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)] +            if 0 <= s < H and 0 <= t < W +        ]: +            if rank[u][v] is not None: -            Grid[nv] = Grid[v] + 1 -            V.append(nv) -            seen.add(nv) -    return 0 +            if maze[u][v] == ".": +                rank[u][v] = rank[x][y] + 1 +                reserved.append((u, v)) +    if rank[H - 1][W - 1] is None: +        print((-1)) +    else: +        counter = Counter() +        for line in maze: +            counter.update(line) +        print((counter["."] - rank[H - 1][W - 1] - 1)) -white = len(Grid[Grid == 0]) - 1 -nes_white = solveMase(Grid, (0, 0), (H - 1, W - 1)) -print((white - nes_white if nes_white > 0 else -1)) +main()