import sys

import queue



sys.setrecursionlimit(1000000)



def s(m, h, w, d, q):

    if q.empty(): return d[h - 1][w - 1]

    k, i, j = q.get()

    if i > 0 and m[i - 1][j] == '.' and k + 1 < d[i - 1][j]:

        d[i - 1][j] = k + 1

        q.put((k + 1, i - 1, j))

    if i < h - 1 and m[i + 1][j] == '.' and k + 1 < d[i + 1][j]:

        d[i + 1][j] = k + 1

        q.put((k + 1, i + 1, j))

    if j > 0 and m[i][j - 1] == '.' and k + 1 < d[i][j - 1]:

        d[i][j - 1] = k + 1

        q.put((k + 1, i, j - 1))

    if j < w - 1 and m[i][j + 1] == '.' and k + 1 < d[i][j + 1]:

        d[i][j + 1] = k + 1

        q.put((k + 1, i, j + 1))

    return s(m, h, w, d, q)



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

m = [input()[:w] for _ in range(h)]

q = queue.PriorityQueue()

d = [None] * h

for i in range(h):

    d[i] = [1000000] * w

d[0][0] = 1

for i in range(h):

    for j in range(w):

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

            q.put((d[i][j], i, j))

c = s(m, h, w, d, q)

if c == 1000000: print(-1)

else:

    a = 0

    for row in m:

        a += row.count('#')

    print(h * w - c - a)

import sys

import queue



sys.setrecursionlimit(1000000)



def s(m, v, q):

    if q.empty(): return -1

    k, i, j = q.get()

    if i == len(m) - 1 and j == len(m[i]) - 1: return k

    if i > 0 and m[i - 1][j] == '.' and (i - 1, j) not in v:

        q.put((k + 1, i - 1, j))

        v.add((i - 1, j))

    if i < len(m) - 1 and m[i + 1][j] == '.' and (i + 1, j) not in v:

        q.put((k + 1, i + 1, j))

        v.add((i + 1, j))

    if j > 0 and m[i][j - 1] == '.' and (i, j - 1) not in v:

        q.put((k + 1, i, j - 1))

        v.add((i, j - 1))

    if j < len(m[i]) - 1 and m[i][j + 1] == '.' and (i, j + 1) not in v:

        q.put((k + 1, i, j + 1))

        v.add((i, j + 1))

    return s(m, v, q)

     

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

m = [input() for _ in range(h)]

q = queue.PriorityQueue()

q.put((1, 0, 0))

v = set((0, 0))

c = s(m, v, q)

if c == -1: print(-1)

else: print(h * w - c - sum([r.count('#') for r in m]))

import sys
import queue

sys.setrecursionlimit(1000000)


def s(m, h, w, d, q):
    if q.empty():
        return d[h - 1][w - 1]
    k, i, j = q.get()
    if i > 0 and m[i - 1][j] == "." and k + 1 < d[i - 1][j]:
        d[i - 1][j] = k + 1
        q.put((k + 1, i - 1, j))
    if i < h - 1 and m[i + 1][j] == "." and k + 1 < d[i + 1][j]:
        d[i + 1][j] = k + 1
        q.put((k + 1, i + 1, j))
    if j > 0 and m[i][j - 1] == "." and k + 1 < d[i][j - 1]:
        d[i][j - 1] = k + 1
        q.put((k + 1, i, j - 1))
    if j < w - 1 and m[i][j + 1] == "." and k + 1 < d[i][j + 1]:
        d[i][j + 1] = k + 1
        q.put((k + 1, i, j + 1))
    return s(m, h, w, d, q)


h, w = list(map(int, input().split()))
m = [input()[:w] for _ in range(h)]
q = queue.PriorityQueue()
d = [None] * h
for i in range(h):
    d[i] = [1000000] * w
d[0][0] = 1
for i in range(h):
    for j in range(w):
        if m[i][j] == ".":
            q.put((d[i][j], i, j))
c = s(m, h, w, d, q)
if c == 1000000:
    print(-1)
else:
    a = 0
    for row in m:
        a += row.count("#")
    print(h * w - c - a)

import sys
import queue

sys.setrecursionlimit(1000000)


def s(m, v, q):
    if q.empty():
        return -1
    k, i, j = q.get()
    if i == len(m) - 1 and j == len(m[i]) - 1:
        return k
    if i > 0 and m[i - 1][j] == "." and (i - 1, j) not in v:
        q.put((k + 1, i - 1, j))
        v.add((i - 1, j))
    if i < len(m) - 1 and m[i + 1][j] == "." and (i + 1, j) not in v:
        q.put((k + 1, i + 1, j))
        v.add((i + 1, j))
    if j > 0 and m[i][j - 1] == "." and (i, j - 1) not in v:
        q.put((k + 1, i, j - 1))
        v.add((i, j - 1))
    if j < len(m[i]) - 1 and m[i][j + 1] == "." and (i, j + 1) not in v:
        q.put((k + 1, i, j + 1))
        v.add((i, j + 1))
    return s(m, v, q)


h, w = list(map(int, input().split()))
m = [input() for _ in range(h)]
q = queue.PriorityQueue()
q.put((1, 0, 0))
v = set((0, 0))
c = s(m, v, q)
if c == -1:
    print(-1)
else:
    print(h * w - c - sum([r.count("#") for r in m]))

-def s(m, h, w, d, q): +def s(m, v, q): -        return d[h - 1][w - 1] +        return -1 -    if i > 0 and m[i - 1][j] == "." and k + 1 < d[i - 1][j]: -        d[i - 1][j] = k + 1 +    if i == len(m) - 1 and j == len(m[i]) - 1: +        return k +    if i > 0 and m[i - 1][j] == "." and (i - 1, j) not in v: -    if i < h - 1 and m[i + 1][j] == "." and k + 1 < d[i + 1][j]: -        d[i + 1][j] = k + 1 +        v.add((i - 1, j)) +    if i < len(m) - 1 and m[i + 1][j] == "." and (i + 1, j) not in v: -    if j > 0 and m[i][j - 1] == "." and k + 1 < d[i][j - 1]: -        d[i][j - 1] = k + 1 +        v.add((i + 1, j)) +    if j > 0 and m[i][j - 1] == "." and (i, j - 1) not in v: -    if j < w - 1 and m[i][j + 1] == "." and k + 1 < d[i][j + 1]: -        d[i][j + 1] = k + 1 +        v.add((i, j - 1)) +    if j < len(m[i]) - 1 and m[i][j + 1] == "." and (i, j + 1) not in v: -    return s(m, h, w, d, q) +        v.add((i, j + 1)) +    return s(m, v, q) -m = [input()[:w] for _ in range(h)] +m = [input() for _ in range(h)] -d = [None] * h -for i in range(h): -    d[i] = [1000000] * w -d[0][0] = 1 -for i in range(h): -    for j in range(w): -        if m[i][j] == ".": -            q.put((d[i][j], i, j)) -c = s(m, h, w, d, q) -if c == 1000000: +q.put((1, 0, 0)) +v = set((0, 0)) +c = s(m, v, q) +if c == -1: -    a = 0 -    for row in m: -        a += row.count("#") -    print(h * w - c - a) +    print(h * w - c - sum([r.count("#") for r in m]))