import sys

sys.setrecursionlimit(3000)

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

s=['#'*(w+2)]+['#'+eval(input())+'#' for _ in [0]*h]+['#'*(w+2)]

l=[[0]*(w+2) for _ in [0]*(h+2)]

for i in range(h+2):

    for j in range(w+2):

        if s[i][j]=='#':

            l[i][j]=-1

def sol(m,n):

    global l

    if l[m][n]>=0:

        for i,j in [(m-1,n),(m+1,n),(m,n-1),(m,n+1)]:

            if l[i][j]==0 and [i,j]!=[1,1]:

                l[i][j]=l[m][n]+1

                sol(i,j)

            elif l[i][j]>l[m][n]+1:

                l[i][j]=l[m][n]+1

                sol(i,j)

sol(1,1)

if l[h][w]==0:print((-1))

else:print((h*w-l[h][w]-(sum(x.count('#') for x in s)-2*h-2*w-4)-1))
import sys

sys.setrecursionlimit(3000)

import queue

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

s=['#'*(w+2)]+['#'+eval(input())+'#' for _ in [0]*h]+['#'*(w+2)]

l=[[0]*(w+2) for _ in [0]*(h+2)]

for i in range(h+2):

    for j in range(w+2):

        if s[i][j]=='#':

            l[i][j]=-1

L=queue.Queue()

L.put([1,1])

while not L.empty():

    n=L.get()

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

        if l[n[0]+dx][n[1]+dy]==0 and [n[0]+dx,n[1]+dy]!=[1,1]:

            l[n[0]+dx][n[1]+dy]=l[n[0]][n[1]]+1

            L.put([n[0]+dx,n[1]+dy])



if l[h][w]==0:print((-1))

else:print((h*w-l[h][w]-(sum(x.count('#') for x in s)-2*h-2*w-4)-1))
import sys

sys.setrecursionlimit(3000)
h, w = list(map(int, input().split()))
s = ["#" * (w + 2)] + ["#" + eval(input()) + "#" for _ in [0] * h] + ["#" * (w + 2)]
l = [[0] * (w + 2) for _ in [0] * (h + 2)]
for i in range(h + 2):
    for j in range(w + 2):
        if s[i][j] == "#":
            l[i][j] = -1


def sol(m, n):
    global l
    if l[m][n] >= 0:
        for i, j in [(m - 1, n), (m + 1, n), (m, n - 1), (m, n + 1)]:
            if l[i][j] == 0 and [i, j] != [1, 1]:
                l[i][j] = l[m][n] + 1
                sol(i, j)
            elif l[i][j] > l[m][n] + 1:
                l[i][j] = l[m][n] + 1
                sol(i, j)


sol(1, 1)
if l[h][w] == 0:
    print((-1))
else:
    print((h * w - l[h][w] - (sum(x.count("#") for x in s) - 2 * h - 2 * w - 4) - 1))

import sys

sys.setrecursionlimit(3000)
import queue

h, w = list(map(int, input().split()))
s = ["#" * (w + 2)] + ["#" + eval(input()) + "#" for _ in [0] * h] + ["#" * (w + 2)]
l = [[0] * (w + 2) for _ in [0] * (h + 2)]
for i in range(h + 2):
    for j in range(w + 2):
        if s[i][j] == "#":
            l[i][j] = -1
L = queue.Queue()
L.put([1, 1])
while not L.empty():
    n = L.get()
    for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        if l[n[0] + dx][n[1] + dy] == 0 and [n[0] + dx, n[1] + dy] != [1, 1]:
            l[n[0] + dx][n[1] + dy] = l[n[0]][n[1]] + 1
            L.put([n[0] + dx, n[1] + dy])
if l[h][w] == 0:
    print((-1))
else:
    print((h * w - l[h][w] - (sum(x.count("#") for x in s) - 2 * h - 2 * w - 4) - 1))

+import queue + - - -def sol(m, n): -    global l -    if l[m][n] >= 0: -        for i, j in [(m - 1, n), (m + 1, n), (m, n - 1), (m, n + 1)]: -            if l[i][j] == 0 and [i, j] != [1, 1]: -                l[i][j] = l[m][n] + 1 -                sol(i, j) -            elif l[i][j] > l[m][n] + 1: -                l[i][j] = l[m][n] + 1 -                sol(i, j) - - -sol(1, 1) +L = queue.Queue() +L.put([1, 1]) +while not L.empty(): +    n = L.get() +    for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: +        if l[n[0] + dx][n[1] + dy] == 0 and [n[0] + dx, n[1] + dy] != [1, 1]: +            l[n[0] + dx][n[1] + dy] = l[n[0]][n[1]] + 1 +            L.put([n[0] + dx, n[1] + dy])