import queue



H,W = list(map(int,input().split()))

s = [ list(eval(input())) for _ in range(H)]



count = 0

for i in range(H):

	for j in range(W):

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

			count += 1



ans = [[0 for _ in range(W)] for _ in range(H)]



q = queue.Queue()



def bfs(x,y):

	global q

	global c

	q.put([x,y])

	s[y][x] = '#'

	while not q.empty():

		x,y = q.get()

		if [x,y] == [W-1,H-1]:

			return ans[H-1][W-1]

		if x > 0 and s[y][x-1] == '.':

			q.put([x-1,y])

			ans[y][x-1] = ans[y][x] + 1

			s[y][x-1] = '#'

		if x < W-1 and s[y][x+1] == '.':

			q.put([x+1,y])

			ans[y][x+1] = ans[y][x] + 1

			s[y][x+1] = '#'

		if y > 0 and s[y-1][x] == '.':

			q.put([x,y-1])

			ans[y-1][x] = ans[y][x] + 1

			s[y-1][x] = '#'

		if y < H-1 and s[y+1][x] == '.':

			q.put([x,y+1])

			ans[y+1][x] = ans[y][x] + 1

			s[y+1][x] = '#'



	return -1



bfs = bfs(0,0)

if bfs == -1:

	print((-1))

else:

	print((count-bfs-1))

from collections import deque



H,W = list(map(int,input().split()))

c =[]

c.append(['#' for _ in range(W+2)])

ans = [[0 for _ in range(W+2)]for _ in range(H+2)]

score = 0

for i in range(H):

    tmp = list('#'+eval(input())+'#')

    c.append(tmp)

    for j in range(W+2):

        if tmp[j] == '.':

            score += 1



c.append(['#' for _ in range(W+2)])

q = deque()



def bfs(x,y):

    q.append([x,y])

    while q:

        x,y = q.popleft()

        if [x,y] == [W,H]:

            return ans[H][W]

        if c[y][x-1] == '.':

            q.append([x-1,y])

            ans[y][x-1] = ans[y][x] + 1

            c[y][x-1] = '#'

        if c[y][x+1] == '.':

            q.append([x+1,y])

            ans[y][x+1] = ans[y][x] + 1

            c[y][x+1] = '#'

        if c[y-1][x] == '.':

            q.append([x,y-1])

            ans[y-1][x] = ans[y][x] + 1

            c[y-1][x] = '#'

        if c[y+1][x] == '.':

            q.append([x,y+1])

            ans[y+1][x] = ans[y][x] + 1

            c[y+1][x] = '#'

    return -1



tmp = bfs(1,1)

if tmp == -1:

    print((-1))

else:

    print((score - tmp - 1))

import queue

H, W = list(map(int, input().split()))
s = [list(eval(input())) for _ in range(H)]
count = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == ".":
            count += 1
ans = [[0 for _ in range(W)] for _ in range(H)]
q = queue.Queue()


def bfs(x, y):
    global q
    global c
    q.put([x, y])
    s[y][x] = "#"
    while not q.empty():
        x, y = q.get()
        if [x, y] == [W - 1, H - 1]:
            return ans[H - 1][W - 1]
        if x > 0 and s[y][x - 1] == ".":
            q.put([x - 1, y])
            ans[y][x - 1] = ans[y][x] + 1
            s[y][x - 1] = "#"
        if x < W - 1 and s[y][x + 1] == ".":
            q.put([x + 1, y])
            ans[y][x + 1] = ans[y][x] + 1
            s[y][x + 1] = "#"
        if y > 0 and s[y - 1][x] == ".":
            q.put([x, y - 1])
            ans[y - 1][x] = ans[y][x] + 1
            s[y - 1][x] = "#"
        if y < H - 1 and s[y + 1][x] == ".":
            q.put([x, y + 1])
            ans[y + 1][x] = ans[y][x] + 1
            s[y + 1][x] = "#"
    return -1


bfs = bfs(0, 0)
if bfs == -1:
    print((-1))
else:
    print((count - bfs - 1))

from collections import deque

H, W = list(map(int, input().split()))
c = []
c.append(["#" for _ in range(W + 2)])
ans = [[0 for _ in range(W + 2)] for _ in range(H + 2)]
score = 0
for i in range(H):
    tmp = list("#" + eval(input()) + "#")
    c.append(tmp)
    for j in range(W + 2):
        if tmp[j] == ".":
            score += 1
c.append(["#" for _ in range(W + 2)])
q = deque()


def bfs(x, y):
    q.append([x, y])
    while q:
        x, y = q.popleft()
        if [x, y] == [W, H]:
            return ans[H][W]
        if c[y][x - 1] == ".":
            q.append([x - 1, y])
            ans[y][x - 1] = ans[y][x] + 1
            c[y][x - 1] = "#"
        if c[y][x + 1] == ".":
            q.append([x + 1, y])
            ans[y][x + 1] = ans[y][x] + 1
            c[y][x + 1] = "#"
        if c[y - 1][x] == ".":
            q.append([x, y - 1])
            ans[y - 1][x] = ans[y][x] + 1
            c[y - 1][x] = "#"
        if c[y + 1][x] == ".":
            q.append([x, y + 1])
            ans[y + 1][x] = ans[y][x] + 1
            c[y + 1][x] = "#"
    return -1


tmp = bfs(1, 1)
if tmp == -1:
    print((-1))
else:
    print((score - tmp - 1))

-import queue +from collections import deque -s = [list(eval(input())) for _ in range(H)] -count = 0 +c = [] +c.append(["#" for _ in range(W + 2)]) +ans = [[0 for _ in range(W + 2)] for _ in range(H + 2)] +score = 0 -    for j in range(W): -        if s[i][j] == ".": -            count += 1 -ans = [[0 for _ in range(W)] for _ in range(H)] -q = queue.Queue() +    tmp = list("#" + eval(input()) + "#") +    c.append(tmp) +    for j in range(W + 2): +        if tmp[j] == ".": +            score += 1 +c.append(["#" for _ in range(W + 2)]) +q = deque() -    global q -    global c -    q.put([x, y]) -    s[y][x] = "#" -    while not q.empty(): -        x, y = q.get() -        if [x, y] == [W - 1, H - 1]: -            return ans[H - 1][W - 1] -        if x > 0 and s[y][x - 1] == ".": -            q.put([x - 1, y]) +    q.append([x, y]) +    while q: +        x, y = q.popleft() +        if [x, y] == [W, H]: +            return ans[H][W] +        if c[y][x - 1] == ".": +            q.append([x - 1, y]) -            s[y][x - 1] = "#" -        if x < W - 1 and s[y][x + 1] == ".": -            q.put([x + 1, y]) +            c[y][x - 1] = "#" +        if c[y][x + 1] == ".": +            q.append([x + 1, y]) -            s[y][x + 1] = "#" -        if y > 0 and s[y - 1][x] == ".": -            q.put([x, y - 1]) +            c[y][x + 1] = "#" +        if c[y - 1][x] == ".": +            q.append([x, y - 1]) -            s[y - 1][x] = "#" -        if y < H - 1 and s[y + 1][x] == ".": -            q.put([x, y + 1]) +            c[y - 1][x] = "#" +        if c[y + 1][x] == ".": +            q.append([x, y + 1]) -            s[y + 1][x] = "#" +            c[y + 1][x] = "#" -bfs = bfs(0, 0) -if bfs == -1: +tmp = bfs(1, 1) +if tmp == -1: -    print((count - bfs - 1)) +    print((score - tmp - 1))