h, w = list(map(int, input().split()))

b = 0

l = [0]*h

for k in range(h):

  a = str(eval(input()))

  for k_ in range(w):

    if a[k_] == '#':

      b += 1

  l[k] = a

#l = [str(input()) for _ in range(h)]

 

from collections import deque

 

queue = deque()

visit = [[-1]*w for _ in range(h)]

queue.append([0, 0])

visit[0][0] = 1

 

while queue:

  y, x = queue.popleft()

  if y == h-1 and x == w-1:

    print((h*w-visit[y][x]-b))

    exit()

  for i, j in ([-1, 0], [1, 0], [0, 1], [0, -1]):

    new_y, new_x = y+i, x+j

    if (new_x>=0) and (new_x<w) and (new_y>=0) and (new_y<h) and l[new_y][new_x]=='.' and visit[new_y][new_x] == -1:

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

      queue.append([new_y,new_x])

    else:

      continue

else:

  print('-1')
h, w = list(map(int, input().split()))



M = [str(eval(input())) for _ in range(h)]



ans = 0

for i in range(h):

    for j in range(w):

        if M[i][j] == '.':

            ans += 1



M = ['#'*w] + M + ['#'*w]

M = ['#'+c+'#' for c in M]



from collections import deque

q = deque()

q.append((1, 1))

visit = [[-1]*(w+2) for _ in range(h+2)]

visit[1][1] = 0



while q:

    y, x = q.popleft()

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

        ny, nx = y+dy, x+dx

        if visit[ny][nx] == -1 and M[ny][nx] != '#':

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

            q.append((ny, nx))



if visit[h][w] == -1:

    print((-1))

else:

    ans = ans -visit[h][w]-1

    print(ans)
h, w = list(map(int, input().split()))
b = 0
l = [0] * h
for k in range(h):
    a = str(eval(input()))
    for k_ in range(w):
        if a[k_] == "#":
            b += 1
    l[k] = a
# l = [str(input()) for _ in range(h)]
from collections import deque

queue = deque()
visit = [[-1] * w for _ in range(h)]
queue.append([0, 0])
visit[0][0] = 1
while queue:
    y, x = queue.popleft()
    if y == h - 1 and x == w - 1:
        print((h * w - visit[y][x] - b))
        exit()
    for i, j in ([-1, 0], [1, 0], [0, 1], [0, -1]):
        new_y, new_x = y + i, x + j
        if (
            (new_x >= 0)
            and (new_x < w)
            and (new_y >= 0)
            and (new_y < h)
            and l[new_y][new_x] == "."
            and visit[new_y][new_x] == -1
        ):
            visit[new_y][new_x] = visit[y][x] + 1
            queue.append([new_y, new_x])
        else:
            continue
else:
    print("-1")

h, w = list(map(int, input().split()))
M = [str(eval(input())) for _ in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if M[i][j] == ".":
            ans += 1
M = ["#" * w] + M + ["#" * w]
M = ["#" + c + "#" for c in M]
from collections import deque

q = deque()
q.append((1, 1))
visit = [[-1] * (w + 2) for _ in range(h + 2)]
visit[1][1] = 0
while q:
    y, x = q.popleft()
    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):
        ny, nx = y + dy, x + dx
        if visit[ny][nx] == -1 and M[ny][nx] != "#":
            visit[ny][nx] = visit[y][x] + 1
            q.append((ny, nx))
if visit[h][w] == -1:
    print((-1))
else:
    ans = ans - visit[h][w] - 1
    print(ans)

-b = 0 -l = [0] * h -for k in range(h): -    a = str(eval(input())) -    for k_ in range(w): -        if a[k_] == "#": -            b += 1 -    l[k] = a -# l = [str(input()) for _ in range(h)] +M = [str(eval(input())) for _ in range(h)] +ans = 0 +for i in range(h): +    for j in range(w): +        if M[i][j] == ".": +            ans += 1 +M = ["#" * w] + M + ["#" * w] +M = ["#" + c + "#" for c in M] -queue = deque() -visit = [[-1] * w for _ in range(h)] -queue.append([0, 0]) -visit[0][0] = 1 -while queue: -    y, x = queue.popleft() -    if y == h - 1 and x == w - 1: -        print((h * w - visit[y][x] - b)) -        exit() -    for i, j in ([-1, 0], [1, 0], [0, 1], [0, -1]): -        new_y, new_x = y + i, x + j -        if ( -            (new_x >= 0) -            and (new_x < w) -            and (new_y >= 0) -            and (new_y < h) -            and l[new_y][new_x] == "." -            and visit[new_y][new_x] == -1 -        ): -            visit[new_y][new_x] = visit[y][x] + 1 -            queue.append([new_y, new_x]) -        else: -            continue +q = deque() +q.append((1, 1)) +visit = [[-1] * (w + 2) for _ in range(h + 2)] +visit[1][1] = 0 +while q: +    y, x = q.popleft() +    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1): +        ny, nx = y + dy, x + dx +        if visit[ny][nx] == -1 and M[ny][nx] != "#": +            visit[ny][nx] = visit[y][x] + 1 +            q.append((ny, nx)) +if visit[h][w] == -1: +    print((-1)) -    print("-1") +    ans = ans - visit[h][w] - 1 +    print(ans)