from queue import Queue



inf = 10 ** 20



def bfs(H, W, s):

    d = [[inf for i in range(W)] for j in range(H)]

    q = Queue()



    d[0][0] = 1

    q.put([0, 0])



    dx = (0, -1, 1, 0)

    dy = (-1, 0, 0, 1)



    while not q.empty():

        p = q.get()

        x = p[0]

        y = p[1]

        for i in range(4):

            nx = x + dx[i]

            ny = y + dy[i]

            if(nx >= 0 and nx < H and ny >= 0 and ny < W):

                if(s[nx][ny] == '.' and d[x][y] + 1 < d[nx][ny]):

                    d[nx][ny] = d[x][y] + 1

                    q.put([nx, ny])



    return d[H-1][W-1]



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

s = []

wnum = 0

for i in range(H):

    s.append(eval(input()))

    for j in range(W):

        if(s[i][j] == "."):

            wnum += 1

d = bfs(H, W, s)

if(d == inf):

    print("-1")

else:

    print((wnum - d))

# https://atcoder.jp/contests/abc088/tasks/abc088_d



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

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

d = [[-1] * W for _ in range(H)]



# 0,0 to h-1, w-1

d[0][0] = 0



queue = []

queue.append([0, 0])



while len(queue) > 0:

    p = queue.pop(0)

    y, x = p[0], p[1]



    dx4 = [0, 0, 1, -1]

    dy4 = [1, -1, 0, 0]



    for dx, dy in zip(dx4, dy4):

        next_y = y + dy

        next_x = x + dx

        if 0 <= next_y < H and 0 <= next_x < W and s[next_y][next_x] != "#" and d[next_y][next_x] == -1:

            d[next_y][next_x] = d[y][x] + 1

            queue.append([next_y, next_x])



count = 0

for l in s:

    for c in l:

        if c == ".":

            count += 1



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

    print((-1))

else:

    print((count - d[H-1][W-1] - 1))



# [print(l) for l in s]

# [print(l) for l in d]

from queue import Queue

inf = 10**20


def bfs(H, W, s):
    d = [[inf for i in range(W)] for j in range(H)]
    q = Queue()
    d[0][0] = 1
    q.put([0, 0])
    dx = (0, -1, 1, 0)
    dy = (-1, 0, 0, 1)
    while not q.empty():
        p = q.get()
        x = p[0]
        y = p[1]
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if nx >= 0 and nx < H and ny >= 0 and ny < W:
                if s[nx][ny] == "." and d[x][y] + 1 < d[nx][ny]:
                    d[nx][ny] = d[x][y] + 1
                    q.put([nx, ny])
    return d[H - 1][W - 1]


H, W = list(map(int, input().split()))
s = []
wnum = 0
for i in range(H):
    s.append(eval(input()))
    for j in range(W):
        if s[i][j] == ".":
            wnum += 1
d = bfs(H, W, s)
if d == inf:
    print("-1")
else:
    print((wnum - d))

# https://atcoder.jp/contests/abc088/tasks/abc088_d
H, W = list(map(int, input().split()))
s = [eval(input()) for _ in range(H)]
d = [[-1] * W for _ in range(H)]
# 0,0 to h-1, w-1
d[0][0] = 0
queue = []
queue.append([0, 0])
while len(queue) > 0:
    p = queue.pop(0)
    y, x = p[0], p[1]
    dx4 = [0, 0, 1, -1]
    dy4 = [1, -1, 0, 0]
    for dx, dy in zip(dx4, dy4):
        next_y = y + dy
        next_x = x + dx
        if (
            0 <= next_y < H
            and 0 <= next_x < W
            and s[next_y][next_x] != "#"
            and d[next_y][next_x] == -1
        ):
            d[next_y][next_x] = d[y][x] + 1
            queue.append([next_y, next_x])
count = 0
for l in s:
    for c in l:
        if c == ".":
            count += 1
if d[H - 1][W - 1] == -1:
    print((-1))
else:
    print((count - d[H - 1][W - 1] - 1))
# [print(l) for l in s]
# [print(l) for l in d]

-from queue import Queue - -inf = 10**20 - - -def bfs(H, W, s): -    d = [[inf for i in range(W)] for j in range(H)] -    q = Queue() -    d[0][0] = 1 -    q.put([0, 0]) -    dx = (0, -1, 1, 0) -    dy = (-1, 0, 0, 1) -    while not q.empty(): -        p = q.get() -        x = p[0] -        y = p[1] -        for i in range(4): -            nx = x + dx[i] -            ny = y + dy[i] -            if nx >= 0 and nx < H and ny >= 0 and ny < W: -                if s[nx][ny] == "." and d[x][y] + 1 < d[nx][ny]: -                    d[nx][ny] = d[x][y] + 1 -                    q.put([nx, ny]) -    return d[H - 1][W - 1] - - +# https://atcoder.jp/contests/abc088/tasks/abc088_d -s = [] -wnum = 0 -for i in range(H): -    s.append(eval(input())) -    for j in range(W): -        if s[i][j] == ".": -            wnum += 1 -d = bfs(H, W, s) -if d == inf: -    print("-1") +s = [eval(input()) for _ in range(H)] +d = [[-1] * W for _ in range(H)] +# 0,0 to h-1, w-1 +d[0][0] = 0 +queue = [] +queue.append([0, 0]) +while len(queue) > 0: +    p = queue.pop(0) +    y, x = p[0], p[1] +    dx4 = [0, 0, 1, -1] +    dy4 = [1, -1, 0, 0] +    for dx, dy in zip(dx4, dy4): +        next_y = y + dy +        next_x = x + dx +        if ( +            0 <= next_y < H +            and 0 <= next_x < W +            and s[next_y][next_x] != "#" +            and d[next_y][next_x] == -1 +        ): +            d[next_y][next_x] = d[y][x] + 1 +            queue.append([next_y, next_x]) +count = 0 +for l in s: +    for c in l: +        if c == ".": +            count += 1 +if d[H - 1][W - 1] == -1: +    print((-1)) -    print((wnum - d)) +    print((count - d[H - 1][W - 1] - 1)) +# [print(l) for l in s] +# [print(l) for l in d]