from collections import deque

r, c = list(map(int, input().split()))

sy, sx = [0, 0]

gy, gx = [r-1, c-1]



s = [list(map(str, list(eval(input())))) for i in range(r)]



N = r*c

G = [[] for _ in range(N)]

w_c = 0

for i in range(r):

    for j in range(c):

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

            w_c += 1

            for x, y in ([0, 1], [0, -1], [1, 0], [-1, 0]):

                if 0 <= i+y <= r-1 and 0 <= j+x <= c-1:

                    if s[i+y][j+x] == '.':

                        G[i*c+j].append((i+y)*c+j+x)





# G[v]: 頂点vに隣接する頂点list

# N: 頂点数

# print(G)

dist = [-1]*N

que = deque([sy*c+sx])

dist[sy*c+sx] = 0

while que:

    v = que.popleft()

    d = dist[v]

    for w in G[v]:

        if dist[w] > -1:

            continue

        dist[w] = d + 1

        que.append(w)

if dist[gy*c+gx] == -1:

    print((-1))

else:

    print((w_c-dist[gy*c+gx]-1))

from collections import defaultdict

from collections import deque

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

s = [list(map(str, list(eval(input())))) for i in range(h)]  # 文字列の二次元配列入力

cnt_sharp = 0

G = defaultdict(lambda: [])

vec = [(0, 1), (0, -1), (1, 0), (-1, 0)]

for i in range(h):

    for j in range(w):

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

            cnt_sharp += 1

        for dy, dx in vec:

            if 0 <= i+dy < h and 0 <= j+dx < w and s[i+dy][j+dx] == '.':

                G[(i+dy, j+dx)].append((i, j))

# G[v]: 頂点vに隣接する頂点list

dist = defaultdict(lambda: -1)

que = deque([(0, 0)])

dist[(0, 0)] = 1

while que:

    v = que.popleft()

    d = dist[v]

    for ww in G[v]:

        if dist[ww] > -1:

            continue

        dist[ww] = d + 1

        que.append(ww)

if dist[(h-1, w-1)] == -1:

    print((-1))

else:

    print((h*w-cnt_sharp-dist[(h-1, w-1)]))

from collections import deque

r, c = list(map(int, input().split()))
sy, sx = [0, 0]
gy, gx = [r - 1, c - 1]
s = [list(map(str, list(eval(input())))) for i in range(r)]
N = r * c
G = [[] for _ in range(N)]
w_c = 0
for i in range(r):
    for j in range(c):
        if s[i][j] == ".":
            w_c += 1
            for x, y in ([0, 1], [0, -1], [1, 0], [-1, 0]):
                if 0 <= i + y <= r - 1 and 0 <= j + x <= c - 1:
                    if s[i + y][j + x] == ".":
                        G[i * c + j].append((i + y) * c + j + x)
# G[v]: 頂点vに隣接する頂点list
# N: 頂点数
# print(G)
dist = [-1] * N
que = deque([sy * c + sx])
dist[sy * c + sx] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)
if dist[gy * c + gx] == -1:
    print((-1))
else:
    print((w_c - dist[gy * c + gx] - 1))

from collections import defaultdict
from collections import deque

h, w = list(map(int, input().split()))
s = [list(map(str, list(eval(input())))) for i in range(h)]  # 文字列の二次元配列入力
cnt_sharp = 0
G = defaultdict(lambda: [])
vec = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            cnt_sharp += 1
        for dy, dx in vec:
            if 0 <= i + dy < h and 0 <= j + dx < w and s[i + dy][j + dx] == ".":
                G[(i + dy, j + dx)].append((i, j))
# G[v]: 頂点vに隣接する頂点list
dist = defaultdict(lambda: -1)
que = deque([(0, 0)])
dist[(0, 0)] = 1
while que:
    v = que.popleft()
    d = dist[v]
    for ww in G[v]:
        if dist[ww] > -1:
            continue
        dist[ww] = d + 1
        que.append(ww)
if dist[(h - 1, w - 1)] == -1:
    print((-1))
else:
    print((h * w - cnt_sharp - dist[(h - 1, w - 1)]))

+from collections import defaultdict -r, c = list(map(int, input().split())) -sy, sx = [0, 0] -gy, gx = [r - 1, c - 1] -s = [list(map(str, list(eval(input())))) for i in range(r)] -N = r * c -G = [[] for _ in range(N)] -w_c = 0 -for i in range(r): -    for j in range(c): -        if s[i][j] == ".": -            w_c += 1 -            for x, y in ([0, 1], [0, -1], [1, 0], [-1, 0]): -                if 0 <= i + y <= r - 1 and 0 <= j + x <= c - 1: -                    if s[i + y][j + x] == ".": -                        G[i * c + j].append((i + y) * c + j + x) +h, w = list(map(int, input().split())) +s = [list(map(str, list(eval(input())))) for i in range(h)]  # 文字列の二次元配列入力 +cnt_sharp = 0 +G = defaultdict(lambda: []) +vec = [(0, 1), (0, -1), (1, 0), (-1, 0)] +for i in range(h): +    for j in range(w): +        if s[i][j] == "#": +            cnt_sharp += 1 +        for dy, dx in vec: +            if 0 <= i + dy < h and 0 <= j + dx < w and s[i + dy][j + dx] == ".": +                G[(i + dy, j + dx)].append((i, j)) -# N: 頂点数 -# print(G) -dist = [-1] * N -que = deque([sy * c + sx]) -dist[sy * c + sx] = 0 +dist = defaultdict(lambda: -1) +que = deque([(0, 0)]) +dist[(0, 0)] = 1 -    for w in G[v]: -        if dist[w] > -1: +    for ww in G[v]: +        if dist[ww] > -1: -        dist[w] = d + 1 -        que.append(w) -if dist[gy * c + gx] == -1: +        dist[ww] = d + 1 +        que.append(ww) +if dist[(h - 1, w - 1)] == -1: -    print((w_c - dist[gy * c + gx] - 1)) +    print((h * w - cnt_sharp - dist[(h - 1, w - 1)]))