import sys

input = sys.stdin.readline

#input = sys.stdin.buffer.readline



#sys.setrecursionlimit(10**9)



def RD(): return sys.stdin.read()

def II(): return int(eval(input()))

def MI(): return list(map(int,input().split()))

def LI(): return list(map(int,input().split()))

def TI(): return tuple(map(int,input().split()))

# rstrip().decode('utf-8')

#import numpy as np



from collections import deque



def main():

	h,w=MI()

	G=["#"]*(w+2)

	for i in range(h):

		G+=["#"]+list(input().rstrip())+["#"]

	G+=["#"]*(w+2)

	ans=0

	#print(G)

	for i in range((h+2)*(w+2)):

		if G[i]==".":

			ans+=1



	Q=deque()

	Q.append((w+3,1))

	G[w+3]="#"

	f=0

	while Q:

		#print(Q)

		x,d=Q.popleft()

		if x==(w+2)*(h+2)-(w+2)-2:

			f=1

			break

		for dx in [-1,1,-w-2,w+2]:

			if G[x+dx]==".":

				Q.append((x+dx,d+1))

				G[x+dx]="#"



	if f==1:

		print((ans-d))

	else:

		print((-1))





















































































































if __name__ == "__main__":

	main()
import sys

input = sys.stdin.buffer.readline



#sys.setrecursionlimit(10**9)

#from functools import lru_cache



def RD(): return input().rstrip().decode()

def II(): return int(eval(input()))

def FI(): return float(eval(input()))

def MI(): return list(map(int,input().split()))

def MF(): return list(map(float,input().split()))

def LI(): return list(map(int,input().split()))

def LF(): return list(map(float,input().split()))

def TI(): return tuple(map(int,input().split()))

# rstrip().decode()



from collections import deque



def main():

	h,w=MI()

	G=[]

	G+=["#"]*(w+2)

	for _ in range(h):

		G+=["#"]+list(RD())+["#"]

	G+=["#"]*(w+2)



	cnt=G.count(".")





	Q=deque()

	Q.append([w+3,1])

	G[w+3]=1



	while Q:

		now,d=Q.popleft()

		for i in [1,-1,w+2,-w-2]:

			if G[now+i]==".":

				G[now+i]=d+1

				Q.append([now+i,d+1])



	ans=-1

	if G[(w+2)*(h+2)-(w+4)]!=".":

		ans=cnt-G[(w+2)*(h+2)-(w+4)]



	print(ans)

























if __name__ == "__main__":

	main()

import sys

input = sys.stdin.readline
# input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**9)
def RD():
    return sys.stdin.read()


def II():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def LI():
    return list(map(int, input().split()))


def TI():
    return tuple(map(int, input().split()))


# rstrip().decode('utf-8')
# import numpy as np
from collections import deque


def main():
    h, w = MI()
    G = ["#"] * (w + 2)
    for i in range(h):
        G += ["#"] + list(input().rstrip()) + ["#"]
    G += ["#"] * (w + 2)
    ans = 0
    # print(G)
    for i in range((h + 2) * (w + 2)):
        if G[i] == ".":
            ans += 1
    Q = deque()
    Q.append((w + 3, 1))
    G[w + 3] = "#"
    f = 0
    while Q:
        # print(Q)
        x, d = Q.popleft()
        if x == (w + 2) * (h + 2) - (w + 2) - 2:
            f = 1
            break
        for dx in [-1, 1, -w - 2, w + 2]:
            if G[x + dx] == ".":
                Q.append((x + dx, d + 1))
                G[x + dx] = "#"
    if f == 1:
        print((ans - d))
    else:
        print((-1))


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**9)
# from functools import lru_cache
def RD():
    return input().rstrip().decode()


def II():
    return int(eval(input()))


def FI():
    return float(eval(input()))


def MI():
    return list(map(int, input().split()))


def MF():
    return list(map(float, input().split()))


def LI():
    return list(map(int, input().split()))


def LF():
    return list(map(float, input().split()))


def TI():
    return tuple(map(int, input().split()))


# rstrip().decode()
from collections import deque


def main():
    h, w = MI()
    G = []
    G += ["#"] * (w + 2)
    for _ in range(h):
        G += ["#"] + list(RD()) + ["#"]
    G += ["#"] * (w + 2)
    cnt = G.count(".")
    Q = deque()
    Q.append([w + 3, 1])
    G[w + 3] = 1
    while Q:
        now, d = Q.popleft()
        for i in [1, -1, w + 2, -w - 2]:
            if G[now + i] == ".":
                G[now + i] = d + 1
                Q.append([now + i, d + 1])
    ans = -1
    if G[(w + 2) * (h + 2) - (w + 4)] != ".":
        ans = cnt - G[(w + 2) * (h + 2) - (w + 4)]
    print(ans)


if __name__ == "__main__":
    main()

-input = sys.stdin.readline -# input = sys.stdin.buffer.readline +input = sys.stdin.buffer.readline +# from functools import lru_cache -    return sys.stdin.read() +    return input().rstrip().decode() +def FI(): +    return float(eval(input())) + + + + +def MF(): +    return list(map(float, input().split())) +def LF(): +    return list(map(float, input().split())) + + -# rstrip().decode('utf-8') -# import numpy as np +# rstrip().decode() -    G = ["#"] * (w + 2) -    for i in range(h): -        G += ["#"] + list(input().rstrip()) + ["#"] +    G = [] -    ans = 0 -    # print(G) -    for i in range((h + 2) * (w + 2)): -        if G[i] == ".": -            ans += 1 +    for _ in range(h): +        G += ["#"] + list(RD()) + ["#"] +    G += ["#"] * (w + 2) +    cnt = G.count(".") -    Q.append((w + 3, 1)) -    G[w + 3] = "#" -    f = 0 +    Q.append([w + 3, 1]) +    G[w + 3] = 1 -        # print(Q) -        x, d = Q.popleft() -        if x == (w + 2) * (h + 2) - (w + 2) - 2: -            f = 1 -            break -        for dx in [-1, 1, -w - 2, w + 2]: -            if G[x + dx] == ".": -                Q.append((x + dx, d + 1)) -                G[x + dx] = "#" -    if f == 1: -        print((ans - d)) -    else: -        print((-1)) +        now, d = Q.popleft() +        for i in [1, -1, w + 2, -w - 2]: +            if G[now + i] == ".": +                G[now + i] = d + 1 +                Q.append([now + i, d + 1]) +    ans = -1 +    if G[(w + 2) * (h + 2) - (w + 4)] != ".": +        ans = cnt - G[(w + 2) * (h + 2) - (w + 4)] +    print(ans)