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()))

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



cnt = 0

for i in range(h):

    cnt += M[i].count('.')



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

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



sy, sx = 1, 1



from collections import deque

stack = deque()

stack.append((sy, sx))

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

visit[sy][sx] = 0



while stack:

    y, x = stack.popleft()

    if y == h and x == w:

        break

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

        new_y, new_x = y+dy, x+dx

        if visit[new_y][new_x] == -1 and M[new_y][new_x] != '#':

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

            stack.append((new_y, new_x))

else:

    print((-1))

    exit()



print((cnt-(visit[h][w]+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()))
M = [str(eval(input())) for _ in range(h)]
cnt = 0
for i in range(h):
    cnt += M[i].count(".")
M = ["#" * w] + M + ["#" * w]
M = ["#" + c + "#" for c in M]
sy, sx = 1, 1
from collections import deque

stack = deque()
stack.append((sy, sx))
visit = [[-1] * (w + 2) for _ in range(h + 2)]
visit[sy][sx] = 0
while stack:
    y, x = stack.popleft()
    if y == h and x == w:
        break
    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):
        new_y, new_x = y + dy, x + dx
        if visit[new_y][new_x] == -1 and M[new_y][new_x] != "#":
            visit[new_y][new_x] = visit[y][x] + 1
            stack.append((new_y, new_x))
else:
    print((-1))
    exit()
print((cnt - (visit[h][w] + 1)))

-ans = 0 +cnt = 0 -    for j in range(w): -        if M[i][j] == ".": -            ans += 1 +    cnt += M[i].count(".") +sy, sx = 1, 1 -q = deque() -q.append((1, 1)) +stack = deque() +stack.append((sy, sx)) -visit[1][1] = 0 -while q: -    y, x = q.popleft() +visit[sy][sx] = 0 +while stack: +    y, x = stack.popleft() +    if y == h and x == w: +        break -        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: +        new_y, new_x = y + dy, x + dx +        if visit[new_y][new_x] == -1 and M[new_y][new_x] != "#": +            visit[new_y][new_x] = visit[y][x] + 1 +            stack.append((new_y, new_x)) +else: -else: -    ans = ans - visit[h][w] - 1 -    print(ans) +    exit() +print((cnt - (visit[h][w] + 1)))