INFTY = 1000

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

S = [["#" for _ in range(W+2)]]

for i in range(H):

    row = list(eval(input()))

    row.insert(0,"#")

    row.append("#")

    S.append(row)

S.append(["#" for _ in range(W+2)])

G = {(i,j):[] for i in range(1,H+1) for j in range(1,W+1)}

for i in range(1,H+1):

    for j in range(1,W+1):

        if S[i][j]==".":

            if S[i][j+1]==".":

                G[(i,j)].append((i,j+1))

            if S[i-1][j]==".":

                G[(i,j)].append((i-1,j))

            if S[i][j-1]==".":

                G[(i,j)].append((i,j-1))

            if S[i+1][j]==".":

                G[(i,j)].append((i+1,j))

tot = 0

for i in range(1,H+1):

    for j in range(1,W+1):

        if S[i][j]==".":

            tot += 1

dist = [[INFTY for j in range(W+2)] for i in range(H+2)]

d = [[0 for j in range(W+2)] for i in range(H+2)]

cur = (1,1)

dist[cur[0]][cur[1]] = 0

d[cur[0]][cur[1]] = 1

while True:

    for x in G[cur]:

        if d[x[0]][x[1]]==0:

            if dist[x[0]][x[1]]>dist[cur[0]][cur[1]]+1:

                dist[x[0]][x[1]] = dist[cur[0]][cur[1]]+1

    dmin = INFTY

    for i in range(1,H+1):

        for j in range(1,W+1):

            if d[i][j]==0:

                if dist[i][j]<dmin:

                    cur = (i,j)

                    dmin = dist[i][j]

    if dmin==INFTY:

        break

    else:

        d[cur[0]][cur[1]]=1

if dist[H][W]==INFTY:

    print((-1))

else:

    print((tot-2-dist[H][W]+1))

    
from collections import deque

INFTY = 10**4

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

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

cnt = 0

for i in range(H):

    for j in range(W):

        if S[i][j]==".":

            cnt += 1

dist = [[INFTY for _ in range(W)] for _ in range(H)]

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

dist[0][0] = 0

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

while que:

    i,j = que.popleft()

    if j+1<W and hist[i][j+1]==0 and S[i][j+1]==".":

        dist[i][j+1] = dist[i][j]+1

        que.append((i,j+1))

        hist[i][j+1] = 1

    if i-1>=0 and hist[i-1][j]==0 and S[i-1][j]==".":

        dist[i-1][j] = dist[i][j]+1

        que.append((i-1,j))

        hist[i-1][j] = 1

    if j-1>=0 and hist[i][j-1]==0 and S[i][j-1]==".":

        dist[i][j-1] = dist[i][j]+1

        que.append((i,j-1))

        hist[i][j-1] = 1

    if i+1<H and hist[i+1][j]==0 and S[i+1][j]==".":

        dist[i+1][j] = dist[i][j]+1

        que.append((i+1,j))

        hist[i+1][j] = 1

if dist[H-1][W-1]==INFTY:

    print((-1))

else:

    print((cnt-2-dist[H-1][W-1]+1))
INFTY = 1000
H, W = list(map(int, input().split()))
S = [["#" for _ in range(W + 2)]]
for i in range(H):
    row = list(eval(input()))
    row.insert(0, "#")
    row.append("#")
    S.append(row)
S.append(["#" for _ in range(W + 2)])
G = {(i, j): [] for i in range(1, H + 1) for j in range(1, W + 1)}
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == ".":
            if S[i][j + 1] == ".":
                G[(i, j)].append((i, j + 1))
            if S[i - 1][j] == ".":
                G[(i, j)].append((i - 1, j))
            if S[i][j - 1] == ".":
                G[(i, j)].append((i, j - 1))
            if S[i + 1][j] == ".":
                G[(i, j)].append((i + 1, j))
tot = 0
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if S[i][j] == ".":
            tot += 1
dist = [[INFTY for j in range(W + 2)] for i in range(H + 2)]
d = [[0 for j in range(W + 2)] for i in range(H + 2)]
cur = (1, 1)
dist[cur[0]][cur[1]] = 0
d[cur[0]][cur[1]] = 1
while True:
    for x in G[cur]:
        if d[x[0]][x[1]] == 0:
            if dist[x[0]][x[1]] > dist[cur[0]][cur[1]] + 1:
                dist[x[0]][x[1]] = dist[cur[0]][cur[1]] + 1
    dmin = INFTY
    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if d[i][j] == 0:
                if dist[i][j] < dmin:
                    cur = (i, j)
                    dmin = dist[i][j]
    if dmin == INFTY:
        break
    else:
        d[cur[0]][cur[1]] = 1
if dist[H][W] == INFTY:
    print((-1))
else:
    print((tot - 2 - dist[H][W] + 1))

from collections import deque

INFTY = 10**4
H, W = list(map(int, input().split()))
S = [input().strip() for _ in range(H)]
cnt = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt += 1
dist = [[INFTY for _ in range(W)] for _ in range(H)]
hist = [[0 for _ in range(W)] for _ in range(H)]
dist[0][0] = 0
que = deque([(0, 0)])
while que:
    i, j = que.popleft()
    if j + 1 < W and hist[i][j + 1] == 0 and S[i][j + 1] == ".":
        dist[i][j + 1] = dist[i][j] + 1
        que.append((i, j + 1))
        hist[i][j + 1] = 1
    if i - 1 >= 0 and hist[i - 1][j] == 0 and S[i - 1][j] == ".":
        dist[i - 1][j] = dist[i][j] + 1
        que.append((i - 1, j))
        hist[i - 1][j] = 1
    if j - 1 >= 0 and hist[i][j - 1] == 0 and S[i][j - 1] == ".":
        dist[i][j - 1] = dist[i][j] + 1
        que.append((i, j - 1))
        hist[i][j - 1] = 1
    if i + 1 < H and hist[i + 1][j] == 0 and S[i + 1][j] == ".":
        dist[i + 1][j] = dist[i][j] + 1
        que.append((i + 1, j))
        hist[i + 1][j] = 1
if dist[H - 1][W - 1] == INFTY:
    print((-1))
else:
    print((cnt - 2 - dist[H - 1][W - 1] + 1))

-INFTY = 1000 +from collections import deque + +INFTY = 10**4 -S = [["#" for _ in range(W + 2)]] +S = [input().strip() for _ in range(H)] +cnt = 0 -    row = list(eval(input())) -    row.insert(0, "#") -    row.append("#") -    S.append(row) -S.append(["#" for _ in range(W + 2)]) -G = {(i, j): [] for i in range(1, H + 1) for j in range(1, W + 1)} -for i in range(1, H + 1): -    for j in range(1, W + 1): +    for j in range(W): -            if S[i][j + 1] == ".": -                G[(i, j)].append((i, j + 1)) -            if S[i - 1][j] == ".": -                G[(i, j)].append((i - 1, j)) -            if S[i][j - 1] == ".": -                G[(i, j)].append((i, j - 1)) -            if S[i + 1][j] == ".": -                G[(i, j)].append((i + 1, j)) -tot = 0 -for i in range(1, H + 1): -    for j in range(1, W + 1): -        if S[i][j] == ".": -            tot += 1 -dist = [[INFTY for j in range(W + 2)] for i in range(H + 2)] -d = [[0 for j in range(W + 2)] for i in range(H + 2)] -cur = (1, 1) -dist[cur[0]][cur[1]] = 0 -d[cur[0]][cur[1]] = 1 -while True: -    for x in G[cur]: -        if d[x[0]][x[1]] == 0: -            if dist[x[0]][x[1]] > dist[cur[0]][cur[1]] + 1: -                dist[x[0]][x[1]] = dist[cur[0]][cur[1]] + 1 -    dmin = INFTY -    for i in range(1, H + 1): -        for j in range(1, W + 1): -            if d[i][j] == 0: -                if dist[i][j] < dmin: -                    cur = (i, j) -                    dmin = dist[i][j] -    if dmin == INFTY: -        break -    else: -        d[cur[0]][cur[1]] = 1 -if dist[H][W] == INFTY: +            cnt += 1 +dist = [[INFTY for _ in range(W)] for _ in range(H)] +hist = [[0 for _ in range(W)] for _ in range(H)] +dist[0][0] = 0 +que = deque([(0, 0)]) +while que: +    i, j = que.popleft() +    if j + 1 < W and hist[i][j + 1] == 0 and S[i][j + 1] == ".": +        dist[i][j + 1] = dist[i][j] + 1 +        que.append((i, j + 1)) +        hist[i][j + 1] = 1 +    if i - 1 >= 0 and hist[i - 1][j] == 0 and S[i - 1][j] == ".": +        dist[i - 1][j] = dist[i][j] + 1 +        que.append((i - 1, j)) +        hist[i - 1][j] = 1 +    if j - 1 >= 0 and hist[i][j - 1] == 0 and S[i][j - 1] == ".": +        dist[i][j - 1] = dist[i][j] + 1 +        que.append((i, j - 1)) +        hist[i][j - 1] = 1 +    if i + 1 < H and hist[i + 1][j] == 0 and S[i + 1][j] == ".": +        dist[i + 1][j] = dist[i][j] + 1 +        que.append((i + 1, j)) +        hist[i + 1][j] = 1 +if dist[H - 1][W - 1] == INFTY: -    print((tot - 2 - dist[H][W] + 1)) +    print((cnt - 2 - dist[H - 1][W - 1] + 1))