def main():

    from queue import Queue

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

    grid=[list(eval(input()))  for i in range(h)]

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

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



    q=Queue()

    q.put([0,0])

    inf=10**5

    ans=[[inf]*w for i in range(h)]

    ans[0][0]=1

    while q.qsize()!=0:

        p=q.get()

        ny,nx=p[0],p[1]

        for i in range(4):

            y=ny+dy[i]

            x=nx+dx[i]    

            if 0<=x<w and 0<=y<h and ans[y][x]>10**4:

                if grid[y][x]=='.':

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

                    q.put([y,x])

    if ans[h-1][w-1]>10**4:

        print((-1))

    else:

        cnt_white=sum([grid[i].count('.') for i in range(h)])

        print((cnt_white-ans[h-1][w-1]))



if __name__=='__main__':

    main()
dy = (-1,0,1,0)

dx = (0,1,0,-1)

from collections import deque



def main():

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

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

    cnt_white = sum(row.count('.') for row in grid)

    

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

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

    dist[0][0] = 1

    while q:

        i,j = q.popleft()

        for k in range(4):

            y = i + dy[k]

            x = j + dx[k]

            if y < 0 or y >= h:

                continue

            if x < 0 or x >= w:

                continue

            if dist[y][x] < 0 and grid[y][x] == '.':

                dist[y][x] = dist[i][j] + 1

                q.append((y,x))

    

    ans = cnt_white - dist[-1][-1]

    print((ans if dist[-1][-1] >= 0 else -1))          

if __name__ == '__main__':

    main()

def main():
    from queue import Queue

    h, w = list(map(int, input().split()))
    grid = [list(eval(input())) for i in range(h)]
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]
    q = Queue()
    q.put([0, 0])
    inf = 10**5
    ans = [[inf] * w for i in range(h)]
    ans[0][0] = 1
    while q.qsize() != 0:
        p = q.get()
        ny, nx = p[0], p[1]
        for i in range(4):
            y = ny + dy[i]
            x = nx + dx[i]
            if 0 <= x < w and 0 <= y < h and ans[y][x] > 10**4:
                if grid[y][x] == ".":
                    ans[y][x] = ans[ny][nx] + 1
                    q.put([y, x])
    if ans[h - 1][w - 1] > 10**4:
        print((-1))
    else:
        cnt_white = sum([grid[i].count(".") for i in range(h)])
        print((cnt_white - ans[h - 1][w - 1]))


if __name__ == "__main__":
    main()

dy = (-1, 0, 1, 0)
dx = (0, 1, 0, -1)
from collections import deque


def main():
    h, w = list(map(int, input().split()))
    grid = [eval(input()) for _ in range(h)]
    cnt_white = sum(row.count(".") for row in grid)
    dist = [[-1] * w for _ in range(h)]
    q = deque([(0, 0)])
    dist[0][0] = 1
    while q:
        i, j = q.popleft()
        for k in range(4):
            y = i + dy[k]
            x = j + dx[k]
            if y < 0 or y >= h:
                continue
            if x < 0 or x >= w:
                continue
            if dist[y][x] < 0 and grid[y][x] == ".":
                dist[y][x] = dist[i][j] + 1
                q.append((y, x))
    ans = cnt_white - dist[-1][-1]
    print((ans if dist[-1][-1] >= 0 else -1))


if __name__ == "__main__":
    main()

+dy = (-1, 0, 1, 0) +dx = (0, 1, 0, -1) +from collections import deque + + -    from queue import Queue - -    grid = [list(eval(input())) for i in range(h)] -    dx = [-1, 0, 1, 0] -    dy = [0, 1, 0, -1] -    q = Queue() -    q.put([0, 0]) -    inf = 10**5 -    ans = [[inf] * w for i in range(h)] -    ans[0][0] = 1 -    while q.qsize() != 0: -        p = q.get() -        ny, nx = p[0], p[1] -        for i in range(4): -            y = ny + dy[i] -            x = nx + dx[i] -            if 0 <= x < w and 0 <= y < h and ans[y][x] > 10**4: -                if grid[y][x] == ".": -                    ans[y][x] = ans[ny][nx] + 1 -                    q.put([y, x]) -    if ans[h - 1][w - 1] > 10**4: -        print((-1)) -    else: -        cnt_white = sum([grid[i].count(".") for i in range(h)]) -        print((cnt_white - ans[h - 1][w - 1])) +    grid = [eval(input()) for _ in range(h)] +    cnt_white = sum(row.count(".") for row in grid) +    dist = [[-1] * w for _ in range(h)] +    q = deque([(0, 0)]) +    dist[0][0] = 1 +    while q: +        i, j = q.popleft() +        for k in range(4): +            y = i + dy[k] +            x = j + dx[k] +            if y < 0 or y >= h: +                continue +            if x < 0 or x >= w: +                continue +            if dist[y][x] < 0 and grid[y][x] == ".": +                dist[y][x] = dist[i][j] + 1 +                q.append((y, x)) +    ans = cnt_white - dist[-1][-1] +    print((ans if dist[-1][-1] >= 0 else -1))