import queue

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

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

q = queue.Queue()

ma = [[0]*w for i in range(h)]

dyx = [[0,1],[0,-1],[1,0],[-1,0]]

white = 0

black = 0

for i in range(h):

    white += s[i].count(".")

black = h*w - white

q.put([0,0])

s[0][0] = "#"

flag = True

while flag:

    newq = queue.Queue()

    while not q.empty():

        y,x = q.get()

        for dy,dx in dyx:

            if 0 <= y + dy< h and 0 <= x +dx <w and s[y+dy][x+dx] != "#":

                s[y+dy][x+dx] = "#"

                newq.put([y+dy,x+dx])

                ma[y+dy][x+dx] = ma[y][x] +1

            if y+dy == h-1 and x+dx == w-1:

                flag = False

    q = newq

    if q.empty():

        break

if ma[h-1][w-1] == 0:

    print((-1))

else:print((white - ma[h-1][w-1]-1))

from collections import deque

def BFS_grid(grid,visited,start,goal):

    y,x = start

    visited[y][x] = 1

    q = deque([start])

    delta = [[-1,0],[0,-1],[1,0],[0,1]]

    h = len(grid)

    w = len(grid[0])

    flag = False

    while q:

        y,x = q.popleft()

        for dy,dx in delta:

            if (0<= x+dx < w) and (0<= y+dy<h) :

                if visited[y+dy][x+dx] == 0 and grid[y+dy][x+dx]==".":

                    visited[y+dy][x+dx] = visited[y][x] +1

                    q.append([y+dy,x+dx])

                    if [y+dy,x+dx] == goal:

                        flag = True

    return flag





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

mz = []

white = 0



for i in range(h):

    mz.append(list(eval(input())))

    white += mz[-1].count(".")



visited = [[0]*w for i in range(h)]

if BFS_grid(mz,visited,[0,0],[h-1,w-1]):

    print((white - (visited[h-1][w-1]-visited[0][0]+1)))

else:

    print((-1))

import queue

h, w = list(map(int, input().split()))
s = [list(eval(input())) for i in range(h)]
q = queue.Queue()
ma = [[0] * w for i in range(h)]
dyx = [[0, 1], [0, -1], [1, 0], [-1, 0]]
white = 0
black = 0
for i in range(h):
    white += s[i].count(".")
black = h * w - white
q.put([0, 0])
s[0][0] = "#"
flag = True
while flag:
    newq = queue.Queue()
    while not q.empty():
        y, x = q.get()
        for dy, dx in dyx:
            if 0 <= y + dy < h and 0 <= x + dx < w and s[y + dy][x + dx] != "#":
                s[y + dy][x + dx] = "#"
                newq.put([y + dy, x + dx])
                ma[y + dy][x + dx] = ma[y][x] + 1
            if y + dy == h - 1 and x + dx == w - 1:
                flag = False
    q = newq
    if q.empty():
        break
if ma[h - 1][w - 1] == 0:
    print((-1))
else:
    print((white - ma[h - 1][w - 1] - 1))

from collections import deque


def BFS_grid(grid, visited, start, goal):
    y, x = start
    visited[y][x] = 1
    q = deque([start])
    delta = [[-1, 0], [0, -1], [1, 0], [0, 1]]
    h = len(grid)
    w = len(grid[0])
    flag = False
    while q:
        y, x = q.popleft()
        for dy, dx in delta:
            if (0 <= x + dx < w) and (0 <= y + dy < h):
                if visited[y + dy][x + dx] == 0 and grid[y + dy][x + dx] == ".":
                    visited[y + dy][x + dx] = visited[y][x] + 1
                    q.append([y + dy, x + dx])
                    if [y + dy, x + dx] == goal:
                        flag = True
    return flag


h, w = list(map(int, input().split()))
mz = []
white = 0
for i in range(h):
    mz.append(list(eval(input())))
    white += mz[-1].count(".")
visited = [[0] * w for i in range(h)]
if BFS_grid(mz, visited, [0, 0], [h - 1, w - 1]):
    print((white - (visited[h - 1][w - 1] - visited[0][0] + 1)))
else:
    print((-1))

-import queue +from collections import deque + + +def BFS_grid(grid, visited, start, goal): +    y, x = start +    visited[y][x] = 1 +    q = deque([start]) +    delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] +    h = len(grid) +    w = len(grid[0]) +    flag = False +    while q: +        y, x = q.popleft() +        for dy, dx in delta: +            if (0 <= x + dx < w) and (0 <= y + dy < h): +                if visited[y + dy][x + dx] == 0 and grid[y + dy][x + dx] == ".": +                    visited[y + dy][x + dx] = visited[y][x] + 1 +                    q.append([y + dy, x + dx]) +                    if [y + dy, x + dx] == goal: +                        flag = True +    return flag + -s = [list(eval(input())) for i in range(h)] -q = queue.Queue() -ma = [[0] * w for i in range(h)] -dyx = [[0, 1], [0, -1], [1, 0], [-1, 0]] +mz = [] -black = 0 -    white += s[i].count(".") -black = h * w - white -q.put([0, 0]) -s[0][0] = "#" -flag = True -while flag: -    newq = queue.Queue() -    while not q.empty(): -        y, x = q.get() -        for dy, dx in dyx: -            if 0 <= y + dy < h and 0 <= x + dx < w and s[y + dy][x + dx] != "#": -                s[y + dy][x + dx] = "#" -                newq.put([y + dy, x + dx]) -                ma[y + dy][x + dx] = ma[y][x] + 1 -            if y + dy == h - 1 and x + dx == w - 1: -                flag = False -    q = newq -    if q.empty(): -        break -if ma[h - 1][w - 1] == 0: +    mz.append(list(eval(input()))) +    white += mz[-1].count(".") +visited = [[0] * w for i in range(h)] +if BFS_grid(mz, visited, [0, 0], [h - 1, w - 1]): +    print((white - (visited[h - 1][w - 1] - visited[0][0] + 1))) +else: -else: -    print((white - ma[h - 1][w - 1] - 1))