from collections import deque

MAX_VAL = 10 ** 9

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

 

 

 

border_row = "#"*(W+2)

field = [border_row]

num_blacks = 0

for i in range(H):

    line = input()

    num_blacks += line.count(".")

    field.append("#"+line+"#")

field.append(border_row)

 

cost = [[MAX_VAL]*(W+2) for _ in range(H+2)]

 

if field[1][1] == ".":

    cost[1][1] = 1

else:

    cost[1][1] = MAX_VAL

 

visited = [[False]*(W+2) for _ in range(H+2)]

dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def bfs():

    q = deque()

    q.append((1, 1))

    visited[1][1] = True

    while len(q):

        x, y = q.popleft()

        for dx, dy in dxdy:

            nx = x + dx

            ny = y + dy

            if field[ny][nx] == "." and not visited[ny][nx]:

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

                visited[ny][nx] = True

                q.append((nx, ny))

    return 

bfs()

print(num_blacks - cost[H][W]) if cost[H][W] < MAX_VAL else print(-1)

import sys

from collections import deque

def input():return sys.stdin.readline().strip()



def main():

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

    

    field = "#" * (W+2)

    field += "#" + "##".join([eval(input()) for _ in range(H)]) + "#"

    field += "#" * (W+2)



    move = [-1, 1, -(W+2), W+2]

    cost = [0] * (H+2) * (W+2)



    def bfs(s):

        q = deque()

        enqueue = q.append

        dequeue = q.popleft

        cost[s] = 1

        enqueue(s)



        while q:

            now = dequeue()

            for dx in move:

                nv = now + dx

                if field[nv] != "#" and cost[nv] <= 0:

                    cost[nv] = cost[now] + 1

                    enqueue(nv)

        

    start = (W+2) + 1

    goal = H*(W+2) + W



    bfs(start)

    white = field.count(".")

    if cost[goal]:

        print((white - cost[goal]))

    else:

        print((-1))

    

if __name__ == "__main__":

    main()
from collections import deque

MAX_VAL = 10**9
H, W = map(int, input().split())
border_row = "#" * (W + 2)
field = [border_row]
num_blacks = 0
for i in range(H):
    line = input()
    num_blacks += line.count(".")
    field.append("#" + line + "#")
field.append(border_row)
cost = [[MAX_VAL] * (W + 2) for _ in range(H + 2)]
if field[1][1] == ".":
    cost[1][1] = 1
else:
    cost[1][1] = MAX_VAL
visited = [[False] * (W + 2) for _ in range(H + 2)]
dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)]


def bfs():
    q = deque()
    q.append((1, 1))
    visited[1][1] = True
    while len(q):
        x, y = q.popleft()
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if field[ny][nx] == "." and not visited[ny][nx]:
                cost[ny][nx] = cost[y][x] + 1
                visited[ny][nx] = True
                q.append((nx, ny))
    return


bfs()
print(num_blacks - cost[H][W]) if cost[H][W] < MAX_VAL else print(-1)

import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


def main():
    H, W = list(map(int, input().split()))
    field = "#" * (W + 2)
    field += "#" + "##".join([eval(input()) for _ in range(H)]) + "#"
    field += "#" * (W + 2)
    move = [-1, 1, -(W + 2), W + 2]
    cost = [0] * (H + 2) * (W + 2)

    def bfs(s):
        q = deque()
        enqueue = q.append
        dequeue = q.popleft
        cost[s] = 1
        enqueue(s)
        while q:
            now = dequeue()
            for dx in move:
                nv = now + dx
                if field[nv] != "#" and cost[nv] <= 0:
                    cost[nv] = cost[now] + 1
                    enqueue(nv)

    start = (W + 2) + 1
    goal = H * (W + 2) + W
    bfs(start)
    white = field.count(".")
    if cost[goal]:
        print((white - cost[goal]))
    else:
        print((-1))


if __name__ == "__main__":
    main()

+import sys -MAX_VAL = 10**9 -H, W = map(int, input().split()) -border_row = "#" * (W + 2) -field = [border_row] -num_blacks = 0 -for i in range(H): -    line = input() -    num_blacks += line.count(".") -    field.append("#" + line + "#") -field.append(border_row) -cost = [[MAX_VAL] * (W + 2) for _ in range(H + 2)] -if field[1][1] == ".": -    cost[1][1] = 1 -else: -    cost[1][1] = MAX_VAL -visited = [[False] * (W + 2) for _ in range(H + 2)] -dxdy = [(-1, 0), (1, 0), (0, -1), (0, 1)] + +def input(): +    return sys.stdin.readline().strip() -def bfs(): -    q = deque() -    q.append((1, 1)) -    visited[1][1] = True -    while len(q): -        x, y = q.popleft() -        for dx, dy in dxdy: -            nx = x + dx -            ny = y + dy -            if field[ny][nx] == "." and not visited[ny][nx]: -                cost[ny][nx] = cost[y][x] + 1 -                visited[ny][nx] = True -                q.append((nx, ny)) -    return +def main(): +    H, W = list(map(int, input().split())) +    field = "#" * (W + 2) +    field += "#" + "##".join([eval(input()) for _ in range(H)]) + "#" +    field += "#" * (W + 2) +    move = [-1, 1, -(W + 2), W + 2] +    cost = [0] * (H + 2) * (W + 2) + +    def bfs(s): +        q = deque() +        enqueue = q.append +        dequeue = q.popleft +        cost[s] = 1 +        enqueue(s) +        while q: +            now = dequeue() +            for dx in move: +                nv = now + dx +                if field[nv] != "#" and cost[nv] <= 0: +                    cost[nv] = cost[now] + 1 +                    enqueue(nv) + +    start = (W + 2) + 1 +    goal = H * (W + 2) + W +    bfs(start) +    white = field.count(".") +    if cost[goal]: +        print((white - cost[goal])) +    else: +        print((-1)) -bfs() -print(num_blacks - cost[H][W]) if cost[H][W] < MAX_VAL else print(-1) +if __name__ == "__main__": +    main()