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

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

inf = 10**6

field = []

d = []

res = 0

for _ in range(h):

    line = list(eval(input()))

    field.append(line)

    d.append([inf]*w)

    for s in line:

        if s==".":

            res += 1



d[0][0] = 0

diff = [

    [1,0]

    ,[0,1]

    ,[-1,0]

    ,[0,-1]

]



flag = True

while flag:

    flag = False

    for hi in range(h):

        for wi in range(w):

            if d[hi][wi]<inf:

                for (dh,dw) in diff:

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

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

                        flag = True



if d[h-1][w-1]<inf:

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

else:

    print((-1))

# -*- 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 -*-
h, w = list(map(int, input().split()))
inf = 10**6
field = []
d = []
res = 0
for _ in range(h):
    line = list(eval(input()))
    field.append(line)
    d.append([inf] * w)
    for s in line:
        if s == ".":
            res += 1
d[0][0] = 0
diff = [[1, 0], [0, 1], [-1, 0], [0, -1]]
flag = True
while flag:
    flag = False
    for hi in range(h):
        for wi in range(w):
            if d[hi][wi] < inf:
                for (dh, dw) in diff:
                    if (
                        h > hi + dh >= 0
                        and w > wi + dw >= 0
                        and field[hi + dh][wi + dw] == "."
                        and d[hi + dh][wi + dw] > d[hi][wi] + 1
                    ):
                        d[hi + dh][wi + dw] = d[hi][wi] + 1
                        flag = True
if d[h - 1][w - 1] < inf:
    print((res - d[h - 1][w - 1] - 1))
else:
    print((-1))

# -*- 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]))

+from queue import Queue + -inf = 10**6 -field = [] +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() -for _ in range(h): -    line = list(eval(input())) -    field.append(line) -    d.append([inf] * w) -    for s in line: -        if s == ".": +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] == ".": -d[0][0] = 0 -diff = [[1, 0], [0, 1], [-1, 0], [0, -1]] -flag = True -while flag: -    flag = False -    for hi in range(h): -        for wi in range(w): -            if d[hi][wi] < inf: -                for (dh, dw) in diff: -                    if ( -                        h > hi + dh >= 0 -                        and w > wi + dw >= 0 -                        and field[hi + dh][wi + dw] == "." -                        and d[hi + dh][wi + dw] > d[hi][wi] + 1 -                    ): -                        d[hi + dh][wi + dw] = d[hi][wi] + 1 -                        flag = True -if d[h - 1][w - 1] < inf: -    print((res - d[h - 1][w - 1] - 1)) -else: -    print((-1)) +print((res - cost[h - 1][w - 1]))