# 迷路読み込み

h,w = list(map(int, input().split()))

maze=[list(eval(input())) for i in range(h)]



ans=0

for i in range(h):

    ans+=maze[i].count('.')



for i in range(h):

    maze[i]=['#']+maze[i]+['#']

maze=[['#' for i in range(w+2)]]+maze+[['#' for i in range(w+2)]]

# 迷路　BFS

import collections

from collections import deque

def clear_maze(sx, sy, gx, gy):



    INF=-10**9

    distance = [[INF for i in range(w+1)] for j in range(h+1)]

    #    distance = [[None]*field_x_length]*field_y_length

    def bfs():

        d = deque()

        d.appendleft((sx, sy))

        distance[sx][sy] = 0



        while len(d):

            x, y = d.pop()

            if x == gx and y == gy:

                break

            for i in range(0, 4):

                nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]



                if (0 <= nx and  0 <= ny  and maze[nx][ny] != '#' and distance[nx][ny] == INF):

                    d.appendleft ((nx, ny))

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



        return distance[gx][gy]



    return bfs()

v=clear_maze(1,1,h,w)



if v==-10**9:

    print((-1))

else:

    print((ans-v-1))
h,w= list(map(int, input().split()))

maze= [list(eval(input())) for i in range(h)]



# 白いマスの数

ans=0

for i in range(h):

    for j in range(w):

        if maze[i][j]=='.':

            ans+=1







import collections

from collections import deque

def clear_maze(sx, sy, gx, gy):



    INF=-10**9

    distance = [[INF for i in range(w+2)] for j in range(h+2)]

    #    distance = [[None]*field_x_length]*field_y_length

    def bfs():

        d = deque()

        d.append((sx, sy))

        distance[sx][sy] = 0



        while len(d):

            x, y = d.popleft()

            if x == gx and y == gy:

                break

            for i in range(0, 4):

                nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]



                if 0 <= nx<h and  0 <= ny<w:

                    if maze[nx][ny] != '#' and distance[nx][ny] == INF:

                        d.append((nx, ny))

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



        return distance[gx][gy]



    return bfs()



v=clear_maze(0,0,h-1,w-1)

INF=-10**9

if v!=INF:

    print((ans-v-1))

else:

    print((-1))
# 迷路読み込み
h, w = list(map(int, input().split()))
maze = [list(eval(input())) for i in range(h)]
ans = 0
for i in range(h):
    ans += maze[i].count(".")
for i in range(h):
    maze[i] = ["#"] + maze[i] + ["#"]
maze = [["#" for i in range(w + 2)]] + maze + [["#" for i in range(w + 2)]]
# 迷路　BFS
import collections
from collections import deque


def clear_maze(sx, sy, gx, gy):
    INF = -(10**9)
    distance = [[INF for i in range(w + 1)] for j in range(h + 1)]
    #    distance = [[None]*field_x_length]*field_y_length
    def bfs():
        d = deque()
        d.appendleft((sx, sy))
        distance[sx][sy] = 0
        while len(d):
            x, y = d.pop()
            if x == gx and y == gy:
                break
            for i in range(0, 4):
                nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]
                if (
                    0 <= nx
                    and 0 <= ny
                    and maze[nx][ny] != "#"
                    and distance[nx][ny] == INF
                ):
                    d.appendleft((nx, ny))
                    distance[nx][ny] = distance[x][y] + 1
        return distance[gx][gy]

    return bfs()


v = clear_maze(1, 1, h, w)
if v == -(10**9):
    print((-1))
else:
    print((ans - v - 1))

h, w = list(map(int, input().split()))
maze = [list(eval(input())) for i in range(h)]
# 白いマスの数
ans = 0
for i in range(h):
    for j in range(w):
        if maze[i][j] == ".":
            ans += 1
import collections
from collections import deque


def clear_maze(sx, sy, gx, gy):
    INF = -(10**9)
    distance = [[INF for i in range(w + 2)] for j in range(h + 2)]
    #    distance = [[None]*field_x_length]*field_y_length
    def bfs():
        d = deque()
        d.append((sx, sy))
        distance[sx][sy] = 0
        while len(d):
            x, y = d.popleft()
            if x == gx and y == gy:
                break
            for i in range(0, 4):
                nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]
                if 0 <= nx < h and 0 <= ny < w:
                    if maze[nx][ny] != "#" and distance[nx][ny] == INF:
                        d.append((nx, ny))
                        distance[nx][ny] = distance[x][y] + 1
        return distance[gx][gy]

    return bfs()


v = clear_maze(0, 0, h - 1, w - 1)
INF = -(10**9)
if v != INF:
    print((ans - v - 1))
else:
    print((-1))

-# 迷路読み込み +# 白いマスの数 -    ans += maze[i].count(".") -for i in range(h): -    maze[i] = ["#"] + maze[i] + ["#"] -maze = [["#" for i in range(w + 2)]] + maze + [["#" for i in range(w + 2)]] -# 迷路　BFS +    for j in range(w): +        if maze[i][j] == ".": +            ans += 1 -    distance = [[INF for i in range(w + 1)] for j in range(h + 1)] +    distance = [[INF for i in range(w + 2)] for j in range(h + 2)] -        d.appendleft((sx, sy)) +        d.append((sx, sy)) -            x, y = d.pop() +            x, y = d.popleft() -                if ( -                    0 <= nx -                    and 0 <= ny -                    and maze[nx][ny] != "#" -                    and distance[nx][ny] == INF -                ): -                    d.appendleft((nx, ny)) -                    distance[nx][ny] = distance[x][y] + 1 +                if 0 <= nx < h and 0 <= ny < w: +                    if maze[nx][ny] != "#" and distance[nx][ny] == INF: +                        d.append((nx, ny)) +                        distance[nx][ny] = distance[x][y] + 1 -v = clear_maze(1, 1, h, w) -if v == -(10**9): +v = clear_maze(0, 0, h - 1, w - 1) +INF = -(10**9) +if v != INF: +    print((ans - v - 1)) +else: -else: -    print((ans - v - 1))