import sys

from pprint import pprint



from collections import deque



h, w = list(map(int, sys.stdin.readline().strip().split(" ")))



field = []

for _ in range(h):

    field.append(list(sys.stdin.readline().strip()))



black = 0

for i in range(h):

    for j in range(w):

        if field[i][j] == "#":

            black += 1



def bfs(field, h, w, sy, sx, gy, gx):

    q = deque()

    q.append((0, sy, sx))

    visited = set()

    while q:

        count, y, x = q.popleft()

        for dy, dx in ((-1, 0), (1, 0), (0, 1), (0, -1)):

            if 0 <= y+dy < h and 0 <= x+dx < w:

                if field[y+dy][x+dx] == "#":

                    continue

                elif (y+dy, x+dx) in visited:

                    continue

                elif (y+dy, x+dx) == (h-1, w-1):

                    return count+1



                visited.add((y+dy, x+dx))

                q.append((count+1, y+dy, x+dx))

    return -1



shortest = bfs(field, h, w, 0, 0, h-1, w-1)

# print(shortest)

if shortest == -1:

    print((-1))

else:

    print((h*w - black - (shortest+1)))
import sys

from collections import deque



H, W = list(map(int, sys.stdin.readline().strip().split()))

grid = [[]] * H

movable = 0

for i in range(H):

    grid[i] = list(sys.stdin.readline().strip())

    for j in grid[i]:

        if j == ".":

            movable += 1

# print(movable)



q = deque()

q.append((0, 0, 0))

visited = set()

goal = False

# BFS

while q:

    x, y, c = q.popleft()

    for dx, dy in ((-1, 0), (0, 1), (1, 0), (0, -1)):

        if 0 <= x+dx < W and 0 <= y+dy < H:

            if x+dx == W-1 and y+dy == H-1:

                goal = True

                c += 1

                break

            elif grid[y+dy][x+dx] == "." and (x+dx, y+dy) not in visited:

                q.append((x+dx, y+dy, c+1))

                visited.add((x+dx, y+dy))



    if goal:

        break



if goal:

    print((movable - (c + 1)))

else:

    print((-1))
import sys
from pprint import pprint
from collections import deque

h, w = list(map(int, sys.stdin.readline().strip().split(" ")))
field = []
for _ in range(h):
    field.append(list(sys.stdin.readline().strip()))
black = 0
for i in range(h):
    for j in range(w):
        if field[i][j] == "#":
            black += 1


def bfs(field, h, w, sy, sx, gy, gx):
    q = deque()
    q.append((0, sy, sx))
    visited = set()
    while q:
        count, y, x = q.popleft()
        for dy, dx in ((-1, 0), (1, 0), (0, 1), (0, -1)):
            if 0 <= y + dy < h and 0 <= x + dx < w:
                if field[y + dy][x + dx] == "#":
                    continue
                elif (y + dy, x + dx) in visited:
                    continue
                elif (y + dy, x + dx) == (h - 1, w - 1):
                    return count + 1
                visited.add((y + dy, x + dx))
                q.append((count + 1, y + dy, x + dx))
    return -1


shortest = bfs(field, h, w, 0, 0, h - 1, w - 1)
# print(shortest)
if shortest == -1:
    print((-1))
else:
    print((h * w - black - (shortest + 1)))

import sys
from collections import deque

H, W = list(map(int, sys.stdin.readline().strip().split()))
grid = [[]] * H
movable = 0
for i in range(H):
    grid[i] = list(sys.stdin.readline().strip())
    for j in grid[i]:
        if j == ".":
            movable += 1
# print(movable)
q = deque()
q.append((0, 0, 0))
visited = set()
goal = False
# BFS
while q:
    x, y, c = q.popleft()
    for dx, dy in ((-1, 0), (0, 1), (1, 0), (0, -1)):
        if 0 <= x + dx < W and 0 <= y + dy < H:
            if x + dx == W - 1 and y + dy == H - 1:
                goal = True
                c += 1
                break
            elif grid[y + dy][x + dx] == "." and (x + dx, y + dy) not in visited:
                q.append((x + dx, y + dy, c + 1))
                visited.add((x + dx, y + dy))
    if goal:
        break
if goal:
    print((movable - (c + 1)))
else:
    print((-1))

-from pprint import pprint -h, w = list(map(int, sys.stdin.readline().strip().split(" "))) -field = [] -for _ in range(h): -    field.append(list(sys.stdin.readline().strip())) -black = 0 -for i in range(h): -    for j in range(w): -        if field[i][j] == "#": -            black += 1 - - -def bfs(field, h, w, sy, sx, gy, gx): -    q = deque() -    q.append((0, sy, sx)) -    visited = set() -    while q: -        count, y, x = q.popleft() -        for dy, dx in ((-1, 0), (1, 0), (0, 1), (0, -1)): -            if 0 <= y + dy < h and 0 <= x + dx < w: -                if field[y + dy][x + dx] == "#": -                    continue -                elif (y + dy, x + dx) in visited: -                    continue -                elif (y + dy, x + dx) == (h - 1, w - 1): -                    return count + 1 -                visited.add((y + dy, x + dx)) -                q.append((count + 1, y + dy, x + dx)) -    return -1 - - -shortest = bfs(field, h, w, 0, 0, h - 1, w - 1) -# print(shortest) -if shortest == -1: +H, W = list(map(int, sys.stdin.readline().strip().split())) +grid = [[]] * H +movable = 0 +for i in range(H): +    grid[i] = list(sys.stdin.readline().strip()) +    for j in grid[i]: +        if j == ".": +            movable += 1 +# print(movable) +q = deque() +q.append((0, 0, 0)) +visited = set() +goal = False +# BFS +while q: +    x, y, c = q.popleft() +    for dx, dy in ((-1, 0), (0, 1), (1, 0), (0, -1)): +        if 0 <= x + dx < W and 0 <= y + dy < H: +            if x + dx == W - 1 and y + dy == H - 1: +                goal = True +                c += 1 +                break +            elif grid[y + dy][x + dx] == "." and (x + dx, y + dy) not in visited: +                q.append((x + dx, y + dy, c + 1)) +                visited.add((x + dx, y + dy)) +    if goal: +        break +if goal: +    print((movable - (c + 1))) +else: -else: -    print((h * w - black - (shortest + 1)))