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

s=[list(eval(input())) for _ in range(h)]



q=[(0,0)]

s[0][0]=0

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

while q:

    x,y=q.pop(0)

    for dx,dy in dxy:

        nx,ny=x+dx,y+dy

        if not 0<=nx<w:

            continue

        if not 0<=ny<h:

            continue

        if s[ny][nx]=="#":

            continue

        if s[ny][nx]==".":

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

            q.append((nx,ny))

        else:

            continue

if s[h-1][w-1]=="." or s[h-1][w-1]=="#":

    print((-1))

else:

    kuro=0

    for i in range(h):

        kuro+=s[i].count("#")



    print((h*w-s[h-1][w-1]-1-kuro))

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

g=[list(eval(input())) for _ in range(h)]

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

from collections import deque

q=deque()

q.append((0,0))

g[0][0]=0

while q:

    ny,nx=q.popleft()

    for dy,dx in dyx:

        y=ny+dy

        x=nx+dx

        if 0<=y<h and 0<=x<w and g[y][x]==".":

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

            q.append((y,x))

if g[h-1][w-1]=="." or g[h-1][w-1]=="#":

    print((-1))

    exit()



d={}

ans=0

x=g[h-1][w-1]

for i in range(h):

    for j in range(w):

        if g[i][j]==".":

            ans+=1

            g[i][j]="@"

            continue

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

            continue

        if g[i][j] in d:

            ans+=1

            g[i][j]="@"

        else:

            if g[i][j]>x:

                ans+=1

                g[i][j]="@"

            d[g[i][j]]=1



print(ans)

h, w = list(map(int, input().split()))
s = [list(eval(input())) for _ in range(h)]
q = [(0, 0)]
s[0][0] = 0
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while q:
    x, y = q.pop(0)
    for dx, dy in dxy:
        nx, ny = x + dx, y + dy
        if not 0 <= nx < w:
            continue
        if not 0 <= ny < h:
            continue
        if s[ny][nx] == "#":
            continue
        if s[ny][nx] == ".":
            s[ny][nx] = s[y][x] + 1
            q.append((nx, ny))
        else:
            continue
if s[h - 1][w - 1] == "." or s[h - 1][w - 1] == "#":
    print((-1))
else:
    kuro = 0
    for i in range(h):
        kuro += s[i].count("#")
    print((h * w - s[h - 1][w - 1] - 1 - kuro))

h, w = list(map(int, input().split()))
g = [list(eval(input())) for _ in range(h)]
dyx = [(1, 0), (-1, 0), (0, 1), (0, -1)]
from collections import deque

q = deque()
q.append((0, 0))
g[0][0] = 0
while q:
    ny, nx = q.popleft()
    for dy, dx in dyx:
        y = ny + dy
        x = nx + dx
        if 0 <= y < h and 0 <= x < w and g[y][x] == ".":
            g[y][x] = 1 + g[ny][nx]
            q.append((y, x))
if g[h - 1][w - 1] == "." or g[h - 1][w - 1] == "#":
    print((-1))
    exit()
d = {}
ans = 0
x = g[h - 1][w - 1]
for i in range(h):
    for j in range(w):
        if g[i][j] == ".":
            ans += 1
            g[i][j] = "@"
            continue
        if g[i][j] == "#":
            continue
        if g[i][j] in d:
            ans += 1
            g[i][j] = "@"
        else:
            if g[i][j] > x:
                ans += 1
                g[i][j] = "@"
            d[g[i][j]] = 1
print(ans)

-s = [list(eval(input())) for _ in range(h)] -q = [(0, 0)] -s[0][0] = 0 -dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)] +g = [list(eval(input())) for _ in range(h)] +dyx = [(1, 0), (-1, 0), (0, 1), (0, -1)] +from collections import deque + +q = deque() +q.append((0, 0)) +g[0][0] = 0 -    x, y = q.pop(0) -    for dx, dy in dxy: -        nx, ny = x + dx, y + dy -        if not 0 <= nx < w: +    ny, nx = q.popleft() +    for dy, dx in dyx: +        y = ny + dy +        x = nx + dx +        if 0 <= y < h and 0 <= x < w and g[y][x] == ".": +            g[y][x] = 1 + g[ny][nx] +            q.append((y, x)) +if g[h - 1][w - 1] == "." or g[h - 1][w - 1] == "#": +    print((-1)) +    exit() +d = {} +ans = 0 +x = g[h - 1][w - 1] +for i in range(h): +    for j in range(w): +        if g[i][j] == ".": +            ans += 1 +            g[i][j] = "@" -        if not 0 <= ny < h: +        if g[i][j] == "#": -        if s[ny][nx] == "#": -            continue -        if s[ny][nx] == ".": -            s[ny][nx] = s[y][x] + 1 -            q.append((nx, ny)) +        if g[i][j] in d: +            ans += 1 +            g[i][j] = "@" -            continue -if s[h - 1][w - 1] == "." or s[h - 1][w - 1] == "#": -    print((-1)) -else: -    kuro = 0 -    for i in range(h): -        kuro += s[i].count("#") -    print((h * w - s[h - 1][w - 1] - 1 - kuro)) +            if g[i][j] > x: +                ans += 1 +                g[i][j] = "@" +            d[g[i][j]] = 1 +print(ans)