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

sx,sy=0,0

gx,gy=w-1,h-1



maze=[]

white=0

for _ in range(h):

    temp=eval(input())

    t=[]

    for s in temp:

        t.append(s)

        if s==".":

            white+=1

    maze.append(t)



maze[sy][sx]=1



from collections import deque

fifo = deque()

fifo.append([sy,sx])



result=-1



while len(fifo)>0:

    temp=fifo.popleft()

    y,x=temp[0],temp[1]



    if y==gy and x==gx:

        result=maze[y][x]



    if y>0:

        if maze[y-1][x]==".":

            fifo.append([y-1,x])

            maze[y-1][x]=maze[y][x]+1

    if y<h-1:

        if maze[y+1][x]==".":

            fifo.append([y+1,x])

            maze[y+1][x]=maze[y][x]+1

    if x>0:

        if maze[y][x-1]==".":

            fifo.append([y,x-1])

            maze[y][x-1]=maze[y][x]+1

    if x<w-1:

        if maze[y][x+1]==".":

            fifo.append([y,x+1])

            maze[y][x+1]=maze[y][x]+1







if result==-1:

    print(result)

else:

    print((white-result))









    



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



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



black_cnt=0

for ss in s:

    black_cnt+=ss.count("#")



from collections import deque

stack=deque([(0,0)])

s[0][0]=1

flag=0

while len(stack)>0:

    #print(stack)

    tmp=stack.popleft()

    if tmp[0]==h-1 and tmp[1]==w-1:

        flag=1

        break



    for (my,mx) in [(1,0),(0,1),(-1,0),(0,-1)]:

        ny=tmp[0]+my

        nx=tmp[1]+mx

        #print(ny,nx)

        if 0<=ny and ny<h and 0<=nx and nx<w:

            #print(ny,nx)

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

                #print(ny,nx,tmp[0],tmp[1],stack)

                s[ny][nx]=s[tmp[0]][tmp[1]]+1

                stack.append((ny,nx))



#for item in s:

 #   print(item)

if flag==1:

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

else:

    print((-1))



    
h, w = list(map(int, input().split()))
sx, sy = 0, 0
gx, gy = w - 1, h - 1
maze = []
white = 0
for _ in range(h):
    temp = eval(input())
    t = []
    for s in temp:
        t.append(s)
        if s == ".":
            white += 1
    maze.append(t)
maze[sy][sx] = 1
from collections import deque

fifo = deque()
fifo.append([sy, sx])
result = -1
while len(fifo) > 0:
    temp = fifo.popleft()
    y, x = temp[0], temp[1]
    if y == gy and x == gx:
        result = maze[y][x]
    if y > 0:
        if maze[y - 1][x] == ".":
            fifo.append([y - 1, x])
            maze[y - 1][x] = maze[y][x] + 1
    if y < h - 1:
        if maze[y + 1][x] == ".":
            fifo.append([y + 1, x])
            maze[y + 1][x] = maze[y][x] + 1
    if x > 0:
        if maze[y][x - 1] == ".":
            fifo.append([y, x - 1])
            maze[y][x - 1] = maze[y][x] + 1
    if x < w - 1:
        if maze[y][x + 1] == ".":
            fifo.append([y, x + 1])
            maze[y][x + 1] = maze[y][x] + 1
if result == -1:
    print(result)
else:
    print((white - result))

h, w = list(map(int, input().split()))
s = [list(eval(input())) for _ in range(h)]
black_cnt = 0
for ss in s:
    black_cnt += ss.count("#")
from collections import deque

stack = deque([(0, 0)])
s[0][0] = 1
flag = 0
while len(stack) > 0:
    # print(stack)
    tmp = stack.popleft()
    if tmp[0] == h - 1 and tmp[1] == w - 1:
        flag = 1
        break
    for (my, mx) in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ny = tmp[0] + my
        nx = tmp[1] + mx
        # print(ny,nx)
        if 0 <= ny and ny < h and 0 <= nx and nx < w:
            # print(ny,nx)
            if s[ny][nx] == ".":
                # print(ny,nx,tmp[0],tmp[1],stack)
                s[ny][nx] = s[tmp[0]][tmp[1]] + 1
                stack.append((ny, nx))
# for item in s:
#   print(item)
if flag == 1:
    print((h * w - s[-1][-1] - black_cnt))
else:
    print((-1))

-sx, sy = 0, 0 -gx, gy = w - 1, h - 1 -maze = [] -white = 0 -for _ in range(h): -    temp = eval(input()) -    t = [] -    for s in temp: -        t.append(s) -        if s == ".": -            white += 1 -    maze.append(t) -maze[sy][sx] = 1 +s = [list(eval(input())) for _ in range(h)] +black_cnt = 0 +for ss in s: +    black_cnt += ss.count("#") -fifo = deque() -fifo.append([sy, sx]) -result = -1 -while len(fifo) > 0: -    temp = fifo.popleft() -    y, x = temp[0], temp[1] -    if y == gy and x == gx: -        result = maze[y][x] -    if y > 0: -        if maze[y - 1][x] == ".": -            fifo.append([y - 1, x]) -            maze[y - 1][x] = maze[y][x] + 1 -    if y < h - 1: -        if maze[y + 1][x] == ".": -            fifo.append([y + 1, x]) -            maze[y + 1][x] = maze[y][x] + 1 -    if x > 0: -        if maze[y][x - 1] == ".": -            fifo.append([y, x - 1]) -            maze[y][x - 1] = maze[y][x] + 1 -    if x < w - 1: -        if maze[y][x + 1] == ".": -            fifo.append([y, x + 1]) -            maze[y][x + 1] = maze[y][x] + 1 -if result == -1: -    print(result) +stack = deque([(0, 0)]) +s[0][0] = 1 +flag = 0 +while len(stack) > 0: +    # print(stack) +    tmp = stack.popleft() +    if tmp[0] == h - 1 and tmp[1] == w - 1: +        flag = 1 +        break +    for (my, mx) in [(1, 0), (0, 1), (-1, 0), (0, -1)]: +        ny = tmp[0] + my +        nx = tmp[1] + mx +        # print(ny,nx) +        if 0 <= ny and ny < h and 0 <= nx and nx < w: +            # print(ny,nx) +            if s[ny][nx] == ".": +                # print(ny,nx,tmp[0],tmp[1],stack) +                s[ny][nx] = s[tmp[0]][tmp[1]] + 1 +                stack.append((ny, nx)) +# for item in s: +#   print(item) +if flag == 1: +    print((h * w - s[-1][-1] - black_cnt)) -    print((white - result)) +    print((-1))