def AppendQue(now, depth, que, past_visit, maze):

    Next = ((now[0] - 1, now[1]), (now[0], now[1] - 1),

            (now[0] + 1, now[1]), (now[0], now[1] + 1))

    for h, w in Next:

        if h < 0 or w < 0 or h >= H or w >= W:

            continue

        elif (h, w) in past_visit:

            continue

        elif maze[h][w] == "#":

            continue

        elif (h, w) in que:

            continue



        que.append((h, w))

        depth[(h, w)] = depth[now] + 1





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



maze = []

black = 0

for i in range(H):

    maze.append(eval(input()))

    black += maze[i].count("#")



if maze[0][0] == "#" or maze[H - 1][W - 1] == "#":

    print((-1))

    exit()



que = []

now = (0, 0)

depth = {}

depth[now] = 1

que.append(now)

past_visit = set()

while True:

    if len(que) == 0:

        print((-1))

        exit()

    now, que = que[0], que[1:]

    if now == (H - 1, W - 1):

        break

    past_visit.add(now)

    AppendQue(now, depth, que, past_visit, maze)



print((H * W - black - depth[(H - 1, W - 1)]))
import sys

from collections import deque

input = sys.stdin.readline

sys.setrecursionlimit(2 * 10**6)





def inpl():

    return list(map(int, input().split()))





H, W = inpl()

S = ["#" + input().strip() + "#" for _ in range(H)]

S = ["#" * (W + 2)] + S + ["#" * (W + 2)]

sharp = sum(len(list([x for x in s if x == "#"])) for s in S)

sharp -= 2 * H + (W + 2) * 2



mp = [[-1] * (W + 2) for i in range(H + 2)]

mp[1][1] = 0

Q = deque()

Q.append((1, 1))

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

while Q:

    h, w = Q.popleft()

    for dh, dw in dhw:

        if S[h + dh][w + dw] == "#":

            continue

        elif mp[h + dh][w + dw] != -1:

            continue

        mp[h + dh][w + dw] = mp[h][w] + 1

        Q.append((h + dh, w + dw))



if mp[H][W] == -1:

    print((-1))

else:

    print((H * W - mp[H][W] - 1 - sharp))

def AppendQue(now, depth, que, past_visit, maze):
    Next = (
        (now[0] - 1, now[1]),
        (now[0], now[1] - 1),
        (now[0] + 1, now[1]),
        (now[0], now[1] + 1),
    )
    for h, w in Next:
        if h < 0 or w < 0 or h >= H or w >= W:
            continue
        elif (h, w) in past_visit:
            continue
        elif maze[h][w] == "#":
            continue
        elif (h, w) in que:
            continue
        que.append((h, w))
        depth[(h, w)] = depth[now] + 1


H, W = list(map(int, input().split()))
maze = []
black = 0
for i in range(H):
    maze.append(eval(input()))
    black += maze[i].count("#")
if maze[0][0] == "#" or maze[H - 1][W - 1] == "#":
    print((-1))
    exit()
que = []
now = (0, 0)
depth = {}
depth[now] = 1
que.append(now)
past_visit = set()
while True:
    if len(que) == 0:
        print((-1))
        exit()
    now, que = que[0], que[1:]
    if now == (H - 1, W - 1):
        break
    past_visit.add(now)
    AppendQue(now, depth, que, past_visit, maze)
print((H * W - black - depth[(H - 1, W - 1)]))

import sys
from collections import deque

input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


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


H, W = inpl()
S = ["#" + input().strip() + "#" for _ in range(H)]
S = ["#" * (W + 2)] + S + ["#" * (W + 2)]
sharp = sum(len(list([x for x in s if x == "#"])) for s in S)
sharp -= 2 * H + (W + 2) * 2
mp = [[-1] * (W + 2) for i in range(H + 2)]
mp[1][1] = 0
Q = deque()
Q.append((1, 1))
dhw = [(1, 0), (0, 1), (-1, 0), (0, -1)]
while Q:
    h, w = Q.popleft()
    for dh, dw in dhw:
        if S[h + dh][w + dw] == "#":
            continue
        elif mp[h + dh][w + dw] != -1:
            continue
        mp[h + dh][w + dw] = mp[h][w] + 1
        Q.append((h + dh, w + dw))
if mp[H][W] == -1:
    print((-1))
else:
    print((H * W - mp[H][W] - 1 - sharp))

-def AppendQue(now, depth, que, past_visit, maze): -    Next = ( -        (now[0] - 1, now[1]), -        (now[0], now[1] - 1), -        (now[0] + 1, now[1]), -        (now[0], now[1] + 1), -    ) -    for h, w in Next: -        if h < 0 or w < 0 or h >= H or w >= W: -            continue -        elif (h, w) in past_visit: -            continue -        elif maze[h][w] == "#": -            continue -        elif (h, w) in que: -            continue -        que.append((h, w)) -        depth[(h, w)] = depth[now] + 1 +import sys +from collections import deque + +input = sys.stdin.readline +sys.setrecursionlimit(2 * 10**6) -H, W = list(map(int, input().split())) -maze = [] -black = 0 -for i in range(H): -    maze.append(eval(input())) -    black += maze[i].count("#") -if maze[0][0] == "#" or maze[H - 1][W - 1] == "#": +def inpl(): +    return list(map(int, input().split())) + + +H, W = inpl() +S = ["#" + input().strip() + "#" for _ in range(H)] +S = ["#" * (W + 2)] + S + ["#" * (W + 2)] +sharp = sum(len(list([x for x in s if x == "#"])) for s in S) +sharp -= 2 * H + (W + 2) * 2 +mp = [[-1] * (W + 2) for i in range(H + 2)] +mp[1][1] = 0 +Q = deque() +Q.append((1, 1)) +dhw = [(1, 0), (0, 1), (-1, 0), (0, -1)] +while Q: +    h, w = Q.popleft() +    for dh, dw in dhw: +        if S[h + dh][w + dw] == "#": +            continue +        elif mp[h + dh][w + dw] != -1: +            continue +        mp[h + dh][w + dw] = mp[h][w] + 1 +        Q.append((h + dh, w + dw)) +if mp[H][W] == -1: -    exit() -que = [] -now = (0, 0) -depth = {} -depth[now] = 1 -que.append(now) -past_visit = set() -while True: -    if len(que) == 0: -        print((-1)) -        exit() -    now, que = que[0], que[1:] -    if now == (H - 1, W - 1): -        break -    past_visit.add(now) -    AppendQue(now, depth, que, past_visit, maze) -print((H * W - black - depth[(H - 1, W - 1)])) +else: +    print((H * W - mp[H][W] - 1 - sharp))