# -*- coding: utf-8 -*-

from queue import Queue

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

d = []

for _ in range(h):

    d.append(eval(input()))

cost = [[0]*w for _ in range(h)]



q = Queue()

q.put((0,0))

while not q.empty():

    hi,wi = q.get()

    if hi==h-1 and wi==w-1:

        break



    for dh,dw in [[0,1],[1,0],[0,-1],[-1,0]]:

        if h>dh+hi>=0 and w>dw+wi>=0 and d[dh+hi][dw+wi]==".":

            if cost[dh+hi][dw+wi]==0 or cost[dh+hi][dw+wi]>cost[hi][wi]+1:

                cost[dh+hi][dw+wi] = cost[hi][wi]+1

                q.put((dh+hi, dw+wi))



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

    print((-1))

    exit()



res = 0

for hi in range(h):

    for wi in range(w):

        if (hi==0 and wi==0) or (hi==h-1 and wi==-1):

            continue

        if d[hi][wi]==".":

            res += 1



print((res - cost[h-1][w-1]))

# -*- coding: utf-8 -*-

from collections import deque

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

m = ["#"*(w+2)] + ["#"+eval(input())+"#" for _ in range(h)] + ["#"*(w+2)]



cnt = 0

for hi in range(1,h+1):

    for wi in range(1,w+1):

        if m[hi][wi]==".":

            cnt += 1



inf = 10**9

d = [[inf]*(w+2) for _ in range(h+2)]

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

d[1][1] = 1

while len(q):

    ch,cw,r = q.popleft()



    for hi,wi in [[ch+1,cw],[ch,cw+1],[ch-1,cw],[ch,cw-1]]:

        if m[hi][wi]=="." and d[hi][wi]>r+1:

            d[hi][wi]=r+1

            q.append((hi,wi,r+1))



if d[h][w]==inf:

    print((-1))

else:

    print((cnt-d[h][w]))
# -*- coding: utf-8 -*-
from queue import Queue

h, w = list(map(int, input().split()))
d = []
for _ in range(h):
    d.append(eval(input()))
cost = [[0] * w for _ in range(h)]
q = Queue()
q.put((0, 0))
while not q.empty():
    hi, wi = q.get()
    if hi == h - 1 and wi == w - 1:
        break
    for dh, dw in [[0, 1], [1, 0], [0, -1], [-1, 0]]:
        if h > dh + hi >= 0 and w > dw + wi >= 0 and d[dh + hi][dw + wi] == ".":
            if cost[dh + hi][dw + wi] == 0 or cost[dh + hi][dw + wi] > cost[hi][wi] + 1:
                cost[dh + hi][dw + wi] = cost[hi][wi] + 1
                q.put((dh + hi, dw + wi))
if cost[h - 1][w - 1] == 0:
    print((-1))
    exit()
res = 0
for hi in range(h):
    for wi in range(w):
        if (hi == 0 and wi == 0) or (hi == h - 1 and wi == -1):
            continue
        if d[hi][wi] == ".":
            res += 1
print((res - cost[h - 1][w - 1]))

# -*- coding: utf-8 -*-
from collections import deque

h, w = list(map(int, input().split()))
m = ["#" * (w + 2)] + ["#" + eval(input()) + "#" for _ in range(h)] + ["#" * (w + 2)]
cnt = 0
for hi in range(1, h + 1):
    for wi in range(1, w + 1):
        if m[hi][wi] == ".":
            cnt += 1
inf = 10**9
d = [[inf] * (w + 2) for _ in range(h + 2)]
q = deque([(1, 1, 1)])
d[1][1] = 1
while len(q):
    ch, cw, r = q.popleft()
    for hi, wi in [[ch + 1, cw], [ch, cw + 1], [ch - 1, cw], [ch, cw - 1]]:
        if m[hi][wi] == "." and d[hi][wi] > r + 1:
            d[hi][wi] = r + 1
            q.append((hi, wi, r + 1))
if d[h][w] == inf:
    print((-1))
else:
    print((cnt - d[h][w]))

-from queue import Queue +from collections import deque -d = [] -for _ in range(h): -    d.append(eval(input())) -cost = [[0] * w for _ in range(h)] -q = Queue() -q.put((0, 0)) -while not q.empty(): -    hi, wi = q.get() -    if hi == h - 1 and wi == w - 1: -        break -    for dh, dw in [[0, 1], [1, 0], [0, -1], [-1, 0]]: -        if h > dh + hi >= 0 and w > dw + wi >= 0 and d[dh + hi][dw + wi] == ".": -            if cost[dh + hi][dw + wi] == 0 or cost[dh + hi][dw + wi] > cost[hi][wi] + 1: -                cost[dh + hi][dw + wi] = cost[hi][wi] + 1 -                q.put((dh + hi, dw + wi)) -if cost[h - 1][w - 1] == 0: +m = ["#" * (w + 2)] + ["#" + eval(input()) + "#" for _ in range(h)] + ["#" * (w + 2)] +cnt = 0 +for hi in range(1, h + 1): +    for wi in range(1, w + 1): +        if m[hi][wi] == ".": +            cnt += 1 +inf = 10**9 +d = [[inf] * (w + 2) for _ in range(h + 2)] +q = deque([(1, 1, 1)]) +d[1][1] = 1 +while len(q): +    ch, cw, r = q.popleft() +    for hi, wi in [[ch + 1, cw], [ch, cw + 1], [ch - 1, cw], [ch, cw - 1]]: +        if m[hi][wi] == "." and d[hi][wi] > r + 1: +            d[hi][wi] = r + 1 +            q.append((hi, wi, r + 1)) +if d[h][w] == inf: -    exit() -res = 0 -for hi in range(h): -    for wi in range(w): -        if (hi == 0 and wi == 0) or (hi == h - 1 and wi == -1): -            continue -        if d[hi][wi] == ".": -            res += 1 -print((res - cost[h - 1][w - 1])) +else: +    print((cnt - d[h][w]))