import sys

from collections import defaultdict



nesw = [[0, 1], [0, -1], [1, 0], [-1, 0]]

height, width = list(map(int, input().split(" ")))

root = []

black = 0

for i in range(height):

    temp = [n for n in eval(input())]

    root.append(temp)

    black += temp.count("#")

start_y, start_x = 0, 0

end_y, end_x = height - 1, width - 1



queue = [(start_y, start_x)]

used_white = 1

flag = 0

while queue:

    new_queue = []

    used_white += 1

    for q in queue:

        h, w = q

        for t in nesw:

            new_h, new_w = h + t[0], w + t[1]

            if 0 <= new_h < height and 0 <= new_w < width:

                if new_h == end_y and new_w == end_x:

                    flag = 1

                    break

                elif root[new_h][new_w] != "#":

                    root[new_h][new_w] = "#"

                    new_queue.append((new_h, new_w))

    if flag:

        break

    queue = new_queue

if flag:

    print((height * width - black - used_white))

else:

    print((-1))
height, width = list(map(int, input().split(" ")))

target = []

white_num = 0

for h in range(height):

    temp = [n for n in eval(input())]

    white_num += temp.count(".")

    target.append(temp)

queue = [[0, 0]]

goal = -1

turn = 1

visited = [[0 for n in range(width)] for m in range(height)]

visited[0][0] = 1

all_move = [[0, 1], [0, -1], [1, 0], [-1, 0]]



while queue:

    next_queue = []

    for q in queue:

        here_x, here_y = q

        if here_x == width - 1 and here_y == height - 1:

            goal = turn

        for move in all_move:

            next_x, next_y = move[0] + here_x, move[1] + here_y

            if 0 <= next_x < width and 0 <= next_y < height:

                if not visited[next_y][next_x] and target[next_y][next_x] == ".":

                    visited[next_y][next_x] = 1

                    next_queue.append([next_x, next_y])

    queue = next_queue

    turn += 1

# print(visited, turn, white_num)

if goal >= 0:

    print((white_num - goal))

else:

    print((-1))
import sys
from collections import defaultdict

nesw = [[0, 1], [0, -1], [1, 0], [-1, 0]]
height, width = list(map(int, input().split(" ")))
root = []
black = 0
for i in range(height):
    temp = [n for n in eval(input())]
    root.append(temp)
    black += temp.count("#")
start_y, start_x = 0, 0
end_y, end_x = height - 1, width - 1
queue = [(start_y, start_x)]
used_white = 1
flag = 0
while queue:
    new_queue = []
    used_white += 1
    for q in queue:
        h, w = q
        for t in nesw:
            new_h, new_w = h + t[0], w + t[1]
            if 0 <= new_h < height and 0 <= new_w < width:
                if new_h == end_y and new_w == end_x:
                    flag = 1
                    break
                elif root[new_h][new_w] != "#":
                    root[new_h][new_w] = "#"
                    new_queue.append((new_h, new_w))
    if flag:
        break
    queue = new_queue
if flag:
    print((height * width - black - used_white))
else:
    print((-1))

height, width = list(map(int, input().split(" ")))
target = []
white_num = 0
for h in range(height):
    temp = [n for n in eval(input())]
    white_num += temp.count(".")
    target.append(temp)
queue = [[0, 0]]
goal = -1
turn = 1
visited = [[0 for n in range(width)] for m in range(height)]
visited[0][0] = 1
all_move = [[0, 1], [0, -1], [1, 0], [-1, 0]]
while queue:
    next_queue = []
    for q in queue:
        here_x, here_y = q
        if here_x == width - 1 and here_y == height - 1:
            goal = turn
        for move in all_move:
            next_x, next_y = move[0] + here_x, move[1] + here_y
            if 0 <= next_x < width and 0 <= next_y < height:
                if not visited[next_y][next_x] and target[next_y][next_x] == ".":
                    visited[next_y][next_x] = 1
                    next_queue.append([next_x, next_y])
    queue = next_queue
    turn += 1
# print(visited, turn, white_num)
if goal >= 0:
    print((white_num - goal))
else:
    print((-1))

-import sys -from collections import defaultdict - -nesw = [[0, 1], [0, -1], [1, 0], [-1, 0]] -root = [] -black = 0 -for i in range(height): +target = [] +white_num = 0 +for h in range(height): -    root.append(temp) -    black += temp.count("#") -start_y, start_x = 0, 0 -end_y, end_x = height - 1, width - 1 -queue = [(start_y, start_x)] -used_white = 1 -flag = 0 +    white_num += temp.count(".") +    target.append(temp) +queue = [[0, 0]] +goal = -1 +turn = 1 +visited = [[0 for n in range(width)] for m in range(height)] +visited[0][0] = 1 +all_move = [[0, 1], [0, -1], [1, 0], [-1, 0]] -    new_queue = [] -    used_white += 1 +    next_queue = [] -        h, w = q -        for t in nesw: -            new_h, new_w = h + t[0], w + t[1] -            if 0 <= new_h < height and 0 <= new_w < width: -                if new_h == end_y and new_w == end_x: -                    flag = 1 -                    break -                elif root[new_h][new_w] != "#": -                    root[new_h][new_w] = "#" -                    new_queue.append((new_h, new_w)) -    if flag: -        break -    queue = new_queue -if flag: -    print((height * width - black - used_white)) +        here_x, here_y = q +        if here_x == width - 1 and here_y == height - 1: +            goal = turn +        for move in all_move: +            next_x, next_y = move[0] + here_x, move[1] + here_y +            if 0 <= next_x < width and 0 <= next_y < height: +                if not visited[next_y][next_x] and target[next_y][next_x] == ".": +                    visited[next_y][next_x] = 1 +                    next_queue.append([next_x, next_y]) +    queue = next_queue +    turn += 1 +# print(visited, turn, white_num) +if goal >= 0: +    print((white_num - goal))