import sys

from collections import deque



import numpy as np



sys.setrecursionlimit(10000)

INF = float('inf')



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

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

S = np.array(S)



# distances[h, w]: (h, w) に行くまでに通る '.' の数

distances = np.empty((H, W), dtype=int)

distances.fill(-1)

que = deque()





def valid(h, w):

    return 0 <= h < H and 0 <= w < W and S[h][w] == '.' and distances[h][w] == -1





# 普通に BFS して通らなかった道を # で埋める

distances[0][0] = 1

que.append((0, 0, H - 1, W - 1))

while len(que) > 0 and distances[-1, -1] == -1:

    sh, sw, eh, ew = que.popleft()

    if valid(sh + 1, sw):

        distances[sh + 1, sw] = distances[sh, sw] + 1

        que.append((sh + 1, sw, eh, ew))

    if valid(sh - 1, sw):

        distances[sh - 1, sw] = distances[sh, sw] + 1

        que.append((sh - 1, sw, eh, ew))

    if valid(sh, sw + 1):

        distances[sh, sw + 1] = distances[sh, sw] + 1

        que.append((sh, sw + 1, eh, ew))

    if valid(sh, sw - 1):

        distances[sh, sw - 1] = distances[sh, sw] + 1

        que.append((sh, sw - 1, eh, ew))



d = distances[-1, -1]

if d == -1:

    print((-1))

else:

    print((H * W - list(S.flat).count('#') - d))

import bisect

import heapq

import itertools

import math

import os

import re

import string

import sys

from collections import Counter, deque, defaultdict

from copy import deepcopy

from decimal import Decimal

from fractions import gcd

from functools import lru_cache, reduce

from operator import itemgetter



import numpy as np



if os.getenv("LOCAL"):

    sys.stdin = open("_in.txt", "r")



sys.setrecursionlimit(2147483647)

INF = float("inf")

IINF = 10 ** 18

MOD = 10 ** 9 + 7



H, W = list(map(int, sys.stdin.readline().split()))

S = [list(sys.stdin.readline().rstrip()) for _ in range(H)]





def ok(h, w):

    return 0 <= h < H and 0 <= w < W and S[h][w] == '.'





dot_count = 0

for r in S:

    dot_count += r.count('.')



# 道の数 - 最短路

sh = sw = 0

que = deque()

que.append((sh, sw, 0))

delta = list(zip(*[[0, 0, 1, -1], [1, -1, 0, 0]]))

distance = -1

while que:

    h, w, d = que.popleft()

    if h == H - 1 and w == W - 1:

        distance = d

        break

    for dh, dw in delta:

        if ok(h + dh, w + dw):

            S[h + dh][w + dw] = '#'

            que.append((h + dh, w + dw, d + 1))



if distance > 0:

    print((dot_count - distance - 1))

else:

    print((-1))

import sys
from collections import deque
import numpy as np

sys.setrecursionlimit(10000)
INF = float("inf")
H, W = list(map(int, input().split()))
S = [list(eval(input())) for _ in range(H)]
S = np.array(S)
# distances[h, w]: (h, w) に行くまでに通る '.' の数
distances = np.empty((H, W), dtype=int)
distances.fill(-1)
que = deque()


def valid(h, w):
    return 0 <= h < H and 0 <= w < W and S[h][w] == "." and distances[h][w] == -1


# 普通に BFS して通らなかった道を # で埋める
distances[0][0] = 1
que.append((0, 0, H - 1, W - 1))
while len(que) > 0 and distances[-1, -1] == -1:
    sh, sw, eh, ew = que.popleft()
    if valid(sh + 1, sw):
        distances[sh + 1, sw] = distances[sh, sw] + 1
        que.append((sh + 1, sw, eh, ew))
    if valid(sh - 1, sw):
        distances[sh - 1, sw] = distances[sh, sw] + 1
        que.append((sh - 1, sw, eh, ew))
    if valid(sh, sw + 1):
        distances[sh, sw + 1] = distances[sh, sw] + 1
        que.append((sh, sw + 1, eh, ew))
    if valid(sh, sw - 1):
        distances[sh, sw - 1] = distances[sh, sw] + 1
        que.append((sh, sw - 1, eh, ew))
d = distances[-1, -1]
if d == -1:
    print((-1))
else:
    print((H * W - list(S.flat).count("#") - d))

import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter
import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
H, W = list(map(int, sys.stdin.readline().split()))
S = [list(sys.stdin.readline().rstrip()) for _ in range(H)]


def ok(h, w):
    return 0 <= h < H and 0 <= w < W and S[h][w] == "."


dot_count = 0
for r in S:
    dot_count += r.count(".")
# 道の数 - 最短路
sh = sw = 0
que = deque()
que.append((sh, sw, 0))
delta = list(zip(*[[0, 0, 1, -1], [1, -1, 0, 0]]))
distance = -1
while que:
    h, w, d = que.popleft()
    if h == H - 1 and w == W - 1:
        distance = d
        break
    for dh, dw in delta:
        if ok(h + dh, w + dw):
            S[h + dh][w + dw] = "#"
            que.append((h + dh, w + dw, d + 1))
if distance > 0:
    print((dot_count - distance - 1))
else:
    print((-1))

+import bisect +import heapq +import itertools +import math +import os +import re +import string -from collections import deque +from collections import Counter, deque, defaultdict +from copy import deepcopy +from decimal import Decimal +from fractions import gcd +from functools import lru_cache, reduce +from operator import itemgetter -sys.setrecursionlimit(10000) +if os.getenv("LOCAL"): +    sys.stdin = open("_in.txt", "r") +sys.setrecursionlimit(2147483647) -H, W = list(map(int, input().split())) -S = [list(eval(input())) for _ in range(H)] -S = np.array(S) -# distances[h, w]: (h, w) に行くまでに通る '.' の数 -distances = np.empty((H, W), dtype=int) -distances.fill(-1) -que = deque() +IINF = 10**18 +MOD = 10**9 + 7 +H, W = list(map(int, sys.stdin.readline().split())) +S = [list(sys.stdin.readline().rstrip()) for _ in range(H)] -def valid(h, w): -    return 0 <= h < H and 0 <= w < W and S[h][w] == "." and distances[h][w] == -1 +def ok(h, w): +    return 0 <= h < H and 0 <= w < W and S[h][w] == "." -# 普通に BFS して通らなかった道を # で埋める -distances[0][0] = 1 -que.append((0, 0, H - 1, W - 1)) -while len(que) > 0 and distances[-1, -1] == -1: -    sh, sw, eh, ew = que.popleft() -    if valid(sh + 1, sw): -        distances[sh + 1, sw] = distances[sh, sw] + 1 -        que.append((sh + 1, sw, eh, ew)) -    if valid(sh - 1, sw): -        distances[sh - 1, sw] = distances[sh, sw] + 1 -        que.append((sh - 1, sw, eh, ew)) -    if valid(sh, sw + 1): -        distances[sh, sw + 1] = distances[sh, sw] + 1 -        que.append((sh, sw + 1, eh, ew)) -    if valid(sh, sw - 1): -        distances[sh, sw - 1] = distances[sh, sw] + 1 -        que.append((sh, sw - 1, eh, ew)) -d = distances[-1, -1] -if d == -1: +dot_count = 0 +for r in S: +    dot_count += r.count(".") +# 道の数 - 最短路 +sh = sw = 0 +que = deque() +que.append((sh, sw, 0)) +delta = list(zip(*[[0, 0, 1, -1], [1, -1, 0, 0]])) +distance = -1 +while que: +    h, w, d = que.popleft() +    if h == H - 1 and w == W - 1: +        distance = d +        break +    for dh, dw in delta: +        if ok(h + dh, w + dw): +            S[h + dh][w + dw] = "#" +            que.append((h + dh, w + dw, d + 1)) +if distance > 0: +    print((dot_count - distance - 1)) +else: -else: -    print((H * W - list(S.flat).count("#") - d))