H, W = list(map(int, input().split()))

s = [[i for i in eval(input())]for i in range(H)]

maze = [[False for i in range(W)] for i in range(H)]

bknum = 0

for i in range(H):

    for j in range(W):

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

            maze[i][j]=True

        else:

            maze[i][j]=False

            bknum += 1





def ncell(i, j):

    tmp = [[i-1,j],[i+1,j],[i,j-1],[i,j+1]]

    out = []

    for p in tmp:

        if 0<=p[0]<H and 0<=p[1]<W and maze[i][j]:

            out.append(p)

    return out



d = [[1000 for j in range(W)] for i in range(H)]

prev = [[-1 for j in range(W)] for i in range(H)]

d[0][0]=0

Q = set()

for i in range(H):

    for j in range(W):

        if maze[i][j]:

            Q.add(i*W+j)

while(len(Q)!=0):

    u = list(Q)[0]

    du = d[u//W][u%W]

    for v in Q:

        if d[v//W][v%W]<du:

            du = d[v//W][v%W]

            u=v

    Q -=set([u])

    for v in ncell(u//W, u%W):

        if d[v[0]][v[1]]>d[u//W][u%W]+1:

            d[v[0]][v[1]]=d[u//W][u%W]+1

            prev[v[0]][v[1]]=u

length=d[H-1][W-1]

if length != 1000:

    print((H*W-length-1-bknum))

else:

    print((-1))

import queue

H,W=list(map(int,input().split()))

s=[list(eval(input())) for i in range(H)]

dist=[[-1 for i in range(W)] for i in range(H)]

q=queue.Queue()

q.put((0,0))

dist[0][0]=0

dx=[-1,1,0,0]

dy=[0,0,1,-1]

while(not(q.empty())):

    r=q.get()

    x,y=r

    for k in range(4):

        nx=x+dx[k]

        ny=y+dy[k]

        if 0<=nx<H and 0<=ny<W:

            if dist[nx][ny]!=-1:

                continue

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

                continue

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

            q.put((nx,ny))

ans=dist[H-1][W-1]

sharp=0

for i in range(H):

    for j in range(W):

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

            sharp+=1

if ans==-1:

    print(ans)

else:

    print((H*W-ans-1-sharp))

H, W = list(map(int, input().split()))
s = [[i for i in eval(input())] for i in range(H)]
maze = [[False for i in range(W)] for i in range(H)]
bknum = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == ".":
            maze[i][j] = True
        else:
            maze[i][j] = False
            bknum += 1


def ncell(i, j):
    tmp = [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]]
    out = []
    for p in tmp:
        if 0 <= p[0] < H and 0 <= p[1] < W and maze[i][j]:
            out.append(p)
    return out


d = [[1000 for j in range(W)] for i in range(H)]
prev = [[-1 for j in range(W)] for i in range(H)]
d[0][0] = 0
Q = set()
for i in range(H):
    for j in range(W):
        if maze[i][j]:
            Q.add(i * W + j)
while len(Q) != 0:
    u = list(Q)[0]
    du = d[u // W][u % W]
    for v in Q:
        if d[v // W][v % W] < du:
            du = d[v // W][v % W]
            u = v
    Q -= set([u])
    for v in ncell(u // W, u % W):
        if d[v[0]][v[1]] > d[u // W][u % W] + 1:
            d[v[0]][v[1]] = d[u // W][u % W] + 1
            prev[v[0]][v[1]] = u
length = d[H - 1][W - 1]
if length != 1000:
    print((H * W - length - 1 - bknum))
else:
    print((-1))

import queue

H, W = list(map(int, input().split()))
s = [list(eval(input())) for i in range(H)]
dist = [[-1 for i in range(W)] for i in range(H)]
q = queue.Queue()
q.put((0, 0))
dist[0][0] = 0
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
while not (q.empty()):
    r = q.get()
    x, y = r
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
        if 0 <= nx < H and 0 <= ny < W:
            if dist[nx][ny] != -1:
                continue
            if s[nx][ny] == "#":
                continue
            dist[nx][ny] = dist[x][y] + 1
            q.put((nx, ny))
ans = dist[H - 1][W - 1]
sharp = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            sharp += 1
if ans == -1:
    print(ans)
else:
    print((H * W - ans - 1 - sharp))

+import queue + -s = [[i for i in eval(input())] for i in range(H)] -maze = [[False for i in range(W)] for i in range(H)] -bknum = 0 +s = [list(eval(input())) for i in range(H)] +dist = [[-1 for i in range(W)] for i in range(H)] +q = queue.Queue() +q.put((0, 0)) +dist[0][0] = 0 +dx = [-1, 1, 0, 0] +dy = [0, 0, 1, -1] +while not (q.empty()): +    r = q.get() +    x, y = r +    for k in range(4): +        nx = x + dx[k] +        ny = y + dy[k] +        if 0 <= nx < H and 0 <= ny < W: +            if dist[nx][ny] != -1: +                continue +            if s[nx][ny] == "#": +                continue +            dist[nx][ny] = dist[x][y] + 1 +            q.put((nx, ny)) +ans = dist[H - 1][W - 1] +sharp = 0 -        if s[i][j] == ".": -            maze[i][j] = True -        else: -            maze[i][j] = False -            bknum += 1 - - -def ncell(i, j): -    tmp = [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]] -    out = [] -    for p in tmp: -        if 0 <= p[0] < H and 0 <= p[1] < W and maze[i][j]: -            out.append(p) -    return out - - -d = [[1000 for j in range(W)] for i in range(H)] -prev = [[-1 for j in range(W)] for i in range(H)] -d[0][0] = 0 -Q = set() -for i in range(H): -    for j in range(W): -        if maze[i][j]: -            Q.add(i * W + j) -while len(Q) != 0: -    u = list(Q)[0] -    du = d[u // W][u % W] -    for v in Q: -        if d[v // W][v % W] < du: -            du = d[v // W][v % W] -            u = v -    Q -= set([u]) -    for v in ncell(u // W, u % W): -        if d[v[0]][v[1]] > d[u // W][u % W] + 1: -            d[v[0]][v[1]] = d[u // W][u % W] + 1 -            prev[v[0]][v[1]] = u -length = d[H - 1][W - 1] -if length != 1000: -    print((H * W - length - 1 - bknum)) +        if s[i][j] == "#": +            sharp += 1 +if ans == -1: +    print(ans) -    print((-1)) +    print((H * W - ans - 1 - sharp))