import sys

from collections import deque

sys.setrecursionlimit(10**7)





def bfs(h, w):  

    for i in dhw:

        dh = i[0]

        dw = i[1]

        if h+dh < 0 or h+dh > H-1 or w+dw < 0 or w+dw > W-1 or field[h+dh][w+dw] == '#':

            continue

        if seen[h+dh][w+dw] == -1:

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

            todo.append([h+dh, w+dw])

        

        

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

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

black_origin = 0

for i in range(H):

    black_origin += field[i].count('#')

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

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

todo = deque()

seen[0][0] = 0

todo.append([0, 0])



while len(todo) > 0:

    n_ad = todo.popleft()

    bfs(n_ad[0], n_ad[1])



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

    print((-1))

else:

    print((H*W-(seen[H-1][W-1]+1)-black_origin))
from collections import deque



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

start = [1, 1]

goal  = [H, W]



maps = [[-1]*(W+2)] + [([-1] + list(eval(input())) + [-1]) for _ in range(H)] + [[-1]*(W+2)]

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

dxy = ((-1, 0), (1, 0), (0, -1), (0, 1))

todo = deque()



blacknum = 0

for i in maps:

    blacknum += i.count('#')





def bfs(x, y):

    for i in dxy:

        new_x = x+i[0]

        new_y = y+i[1]

        if seen[new_x][new_y] == -1:

            if maps[new_x][new_y] == '.':

                seen[new_x][new_y] = seen[x][y] + 1

                todo.append([new_x, new_y])

            else:

                seen[new_x][new_y] = -2





todo.append([start[0], start[1]])

seen[start[0]][start[1]] = 1



while len(todo) > 0:

    tmp = todo.popleft()

    bfs(tmp[0], tmp[1])



if seen[H][W] < 0:

    print((-1))

else:

    print((H*W-blacknum-seen[H][W]))
import sys
from collections import deque

sys.setrecursionlimit(10**7)


def bfs(h, w):
    for i in dhw:
        dh = i[0]
        dw = i[1]
        if (
            h + dh < 0
            or h + dh > H - 1
            or w + dw < 0
            or w + dw > W - 1
            or field[h + dh][w + dw] == "#"
        ):
            continue
        if seen[h + dh][w + dw] == -1:
            seen[h + dh][w + dw] = seen[h][w] + 1
            todo.append([h + dh, w + dw])


H, W = list(map(int, input().split()))
field = [list(eval(input())) for _ in range(H)]
black_origin = 0
for i in range(H):
    black_origin += field[i].count("#")
dhw = ((-1, 0), (1, 0), (0, -1), (0, 1))
seen = [[-1] * W for _ in range(H)]
todo = deque()
seen[0][0] = 0
todo.append([0, 0])
while len(todo) > 0:
    n_ad = todo.popleft()
    bfs(n_ad[0], n_ad[1])
if seen[H - 1][W - 1] == -1:
    print((-1))
else:
    print((H * W - (seen[H - 1][W - 1] + 1) - black_origin))

from collections import deque

H, W = list(map(int, input().split()))
start = [1, 1]
goal = [H, W]
maps = (
    [[-1] * (W + 2)]
    + [([-1] + list(eval(input())) + [-1]) for _ in range(H)]
    + [[-1] * (W + 2)]
)
seen = [[-1] * (W + 2) for _ in range(H + 2)]
dxy = ((-1, 0), (1, 0), (0, -1), (0, 1))
todo = deque()
blacknum = 0
for i in maps:
    blacknum += i.count("#")


def bfs(x, y):
    for i in dxy:
        new_x = x + i[0]
        new_y = y + i[1]
        if seen[new_x][new_y] == -1:
            if maps[new_x][new_y] == ".":
                seen[new_x][new_y] = seen[x][y] + 1
                todo.append([new_x, new_y])
            else:
                seen[new_x][new_y] = -2


todo.append([start[0], start[1]])
seen[start[0]][start[1]] = 1
while len(todo) > 0:
    tmp = todo.popleft()
    bfs(tmp[0], tmp[1])
if seen[H][W] < 0:
    print((-1))
else:
    print((H * W - blacknum - seen[H][W]))

-import sys -sys.setrecursionlimit(10**7) +H, W = list(map(int, input().split())) +start = [1, 1] +goal = [H, W] +maps = ( +    [[-1] * (W + 2)] +    + [([-1] + list(eval(input())) + [-1]) for _ in range(H)] +    + [[-1] * (W + 2)] +) +seen = [[-1] * (W + 2) for _ in range(H + 2)] +dxy = ((-1, 0), (1, 0), (0, -1), (0, 1)) +todo = deque() +blacknum = 0 +for i in maps: +    blacknum += i.count("#") -def bfs(h, w): -    for i in dhw: -        dh = i[0] -        dw = i[1] -        if ( -            h + dh < 0 -            or h + dh > H - 1 -            or w + dw < 0 -            or w + dw > W - 1 -            or field[h + dh][w + dw] == "#" -        ): -            continue -        if seen[h + dh][w + dw] == -1: -            seen[h + dh][w + dw] = seen[h][w] + 1 -            todo.append([h + dh, w + dw]) +def bfs(x, y): +    for i in dxy: +        new_x = x + i[0] +        new_y = y + i[1] +        if seen[new_x][new_y] == -1: +            if maps[new_x][new_y] == ".": +                seen[new_x][new_y] = seen[x][y] + 1 +                todo.append([new_x, new_y]) +            else: +                seen[new_x][new_y] = -2 -H, W = list(map(int, input().split())) -field = [list(eval(input())) for _ in range(H)] -black_origin = 0 -for i in range(H): -    black_origin += field[i].count("#") -dhw = ((-1, 0), (1, 0), (0, -1), (0, 1)) -seen = [[-1] * W for _ in range(H)] -todo = deque() -seen[0][0] = 0 -todo.append([0, 0]) +todo.append([start[0], start[1]]) +seen[start[0]][start[1]] = 1 -    n_ad = todo.popleft() -    bfs(n_ad[0], n_ad[1]) -if seen[H - 1][W - 1] == -1: +    tmp = todo.popleft() +    bfs(tmp[0], tmp[1]) +if seen[H][W] < 0: -    print((H * W - (seen[H - 1][W - 1] + 1) - black_origin)) +    print((H * W - blacknum - seen[H][W]))