from queue import Queue



#入力

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



#lst = [['a']*W for _ in range(H)]

count = 0 #白色のマスの総数

lst = []

row = []

for i in range(H):

    row = list(eval(input()))

    count += row.count('.')

    lst += [row]

 

#for i in range(H):

#    for j in range(W):

#        if lst[i][j] == '.':

#            count += 1



#sx, sy = 1, 1 #スタート位置

#gx, gy = H, W #ゴール位置



move = [[-1, 0], [1,0], [0, -1], [0, 1]]



def bfs():

    q = Queue()

    INF = 10 ** 9

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

    q.put(0)

    q.put(0)

    d[0][0] = 0



    while True:

        if q.empty():

            break

        else:

            px = q.get()

            py = q.get()

            if px == H-1 and py == W-1:

                break

            else:

                for dx, dy in move:

                    nx = px + dx

                    ny = py + dy



                    if 0 <= nx and nx < H and 0 <= ny and ny < W and lst[nx][ny] == '.' and d[nx][ny] == INF:

                        q.put(nx)

                        q.put(ny)

                        d[nx][ny] = d[px][py] + 1

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



a = bfs()



if a == 10 **9:

    print((-1))

else:

    print((count - a - 1))



from collections import deque



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



count = 0

g = []

g.append(['#'] * (W + 2))

for _ in range(H):

    lst = ['#'] + list(eval(input())) + ['#']

    count += lst.count('.')

    g.append(lst)



g.append(['#'] * (W + 2))

check = [[-1] * (W + 2) for _ in range(H + 2)]



move = [[1, 0], [-1, 0], [0, 1], [0, -1]]





# for G in g:

#     print (G)

x = 1

y = 1

que = deque()

que.append([x, y])

# stack = [[x, y]]

flag = True

check[1][1] = 1

while que:

    tmp = que.popleft()

    x = tmp[0]

    y = tmp[1]

    # print (x, y)

    for dx, dy in move:

        nx = x + dx

        ny = y + dy

        # print (nx, ny)

        if g[nx][ny] == '.' and check[nx][ny] == -1:

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

            que.append([nx, ny])

            # print ('OK')



# for c in check:

#     print (c)



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

    print((-1))

    exit()



print((count - check[H][W]))
from queue import Queue

# 入力
H, W = list(map(int, input().split()))
# lst = [['a']*W for _ in range(H)]
count = 0  # 白色のマスの総数
lst = []
row = []
for i in range(H):
    row = list(eval(input()))
    count += row.count(".")
    lst += [row]
# for i in range(H):
#    for j in range(W):
#        if lst[i][j] == '.':
#            count += 1
# sx, sy = 1, 1 #スタート位置
# gx, gy = H, W #ゴール位置
move = [[-1, 0], [1, 0], [0, -1], [0, 1]]


def bfs():
    q = Queue()
    INF = 10**9
    d = [[INF] * W for _ in range(H)]
    q.put(0)
    q.put(0)
    d[0][0] = 0
    while True:
        if q.empty():
            break
        else:
            px = q.get()
            py = q.get()
            if px == H - 1 and py == W - 1:
                break
            else:
                for dx, dy in move:
                    nx = px + dx
                    ny = py + dy
                    if (
                        0 <= nx
                        and nx < H
                        and 0 <= ny
                        and ny < W
                        and lst[nx][ny] == "."
                        and d[nx][ny] == INF
                    ):
                        q.put(nx)
                        q.put(ny)
                        d[nx][ny] = d[px][py] + 1
    return d[H - 1][W - 1]


a = bfs()
if a == 10**9:
    print((-1))
else:
    print((count - a - 1))

from collections import deque

H, W = list(map(int, input().split()))
count = 0
g = []
g.append(["#"] * (W + 2))
for _ in range(H):
    lst = ["#"] + list(eval(input())) + ["#"]
    count += lst.count(".")
    g.append(lst)
g.append(["#"] * (W + 2))
check = [[-1] * (W + 2) for _ in range(H + 2)]
move = [[1, 0], [-1, 0], [0, 1], [0, -1]]
# for G in g:
#     print (G)
x = 1
y = 1
que = deque()
que.append([x, y])
# stack = [[x, y]]
flag = True
check[1][1] = 1
while que:
    tmp = que.popleft()
    x = tmp[0]
    y = tmp[1]
    # print (x, y)
    for dx, dy in move:
        nx = x + dx
        ny = y + dy
        # print (nx, ny)
        if g[nx][ny] == "." and check[nx][ny] == -1:
            check[nx][ny] = check[x][y] + 1
            que.append([nx, ny])
            # print ('OK')
# for c in check:
#     print (c)
if check[H][W] == -1:
    print((-1))
    exit()
print((count - check[H][W]))

-from queue import Queue +from collections import deque -# 入力 -# lst = [['a']*W for _ in range(H)] -count = 0  # 白色のマスの総数 -lst = [] -row = [] -for i in range(H): -    row = list(eval(input())) -    count += row.count(".") -    lst += [row] -# for i in range(H): -#    for j in range(W): -#        if lst[i][j] == '.': -#            count += 1 -# sx, sy = 1, 1 #スタート位置 -# gx, gy = H, W #ゴール位置 -move = [[-1, 0], [1, 0], [0, -1], [0, 1]] - - -def bfs(): -    q = Queue() -    INF = 10**9 -    d = [[INF] * W for _ in range(H)] -    q.put(0) -    q.put(0) -    d[0][0] = 0 -    while True: -        if q.empty(): -            break -        else: -            px = q.get() -            py = q.get() -            if px == H - 1 and py == W - 1: -                break -            else: -                for dx, dy in move: -                    nx = px + dx -                    ny = py + dy -                    if ( -                        0 <= nx -                        and nx < H -                        and 0 <= ny -                        and ny < W -                        and lst[nx][ny] == "." -                        and d[nx][ny] == INF -                    ): -                        q.put(nx) -                        q.put(ny) -                        d[nx][ny] = d[px][py] + 1 -    return d[H - 1][W - 1] - - -a = bfs() -if a == 10**9: +count = 0 +g = [] +g.append(["#"] * (W + 2)) +for _ in range(H): +    lst = ["#"] + list(eval(input())) + ["#"] +    count += lst.count(".") +    g.append(lst) +g.append(["#"] * (W + 2)) +check = [[-1] * (W + 2) for _ in range(H + 2)] +move = [[1, 0], [-1, 0], [0, 1], [0, -1]] +# for G in g: +#     print (G) +x = 1 +y = 1 +que = deque() +que.append([x, y]) +# stack = [[x, y]] +flag = True +check[1][1] = 1 +while que: +    tmp = que.popleft() +    x = tmp[0] +    y = tmp[1] +    # print (x, y) +    for dx, dy in move: +        nx = x + dx +        ny = y + dy +        # print (nx, ny) +        if g[nx][ny] == "." and check[nx][ny] == -1: +            check[nx][ny] = check[x][y] + 1 +            que.append([nx, ny]) +            # print ('OK') +# for c in check: +#     print (c) +if check[H][W] == -1: -else: -    print((count - a - 1)) +    exit() +print((count - check[H][W]))