import queue



def solve():

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



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



    dist = [[h*w*100 for i in range(w)] for j in range(h)]

    dist[0][0] = 1



    q = queue.Queue()

    q.put((0,0))



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

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

    while not q.empty() :

        (x,y) = q.get()

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

            nx = x + dx

            ny = y + dy

            if nx < 0 or nx >= h or ny < 0 or ny>= w :

                continue

            if field[nx][ny] == '#' :

                continue

            if dist[nx][ny] > dist[x][y] + 1 :

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

                q.put((nx,ny))



    if dist[h-1][w-1] >= h*w :

        return -1



    # print(dist)



    ans = h*w - dist[h-1][w-1]

    for v in field:

        for c in v:

            if c == '#':

                ans -= 1



    return ans



if __name__ == '__main__':

    print((solve()))

import queue



def solve():

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



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



    dist = [[h*w*100 for i in range(w)] for j in range(h)]

    dist[0][0] = 1



    q = queue.Queue()

    q.put((0,0))



    while not q.empty() :

        (x,y) = q.get()

        nd = dist[x][y] + 1

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

            nx = x + dx

            ny = y + dy

            if nx < 0 or nx >= h or ny < 0 or ny>= w :

                continue

            if field[nx][ny] == '.' and dist[nx][ny] > nd :

                dist[nx][ny] = nd

                q.put((nx,ny))



    if dist[h-1][w-1] >= h*w :

        return -1



    ans = h*w - dist[h-1][w-1]

    for v in field:

        ans -= v.count('#')



    return ans



if __name__ == '__main__':

    print((solve()))

import queue


def solve():
    h, w = list(map(int, input().split()))
    field = [eval(input()) for _ in range(h)]
    dist = [[h * w * 100 for i in range(w)] for j in range(h)]
    dist[0][0] = 1
    q = queue.Queue()
    q.put((0, 0))
    dx = [-1, 1, 0, 0]
    dy = [0, 0, 1, -1]
    while not q.empty():
        (x, y) = q.get()
        for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                continue
            if field[nx][ny] == "#":
                continue
            if dist[nx][ny] > dist[x][y] + 1:
                dist[nx][ny] = dist[x][y] + 1
                q.put((nx, ny))
    if dist[h - 1][w - 1] >= h * w:
        return -1
    # print(dist)
    ans = h * w - dist[h - 1][w - 1]
    for v in field:
        for c in v:
            if c == "#":
                ans -= 1
    return ans


if __name__ == "__main__":
    print((solve()))

import queue


def solve():
    h, w = list(map(int, input().split()))
    field = [eval(input()) for _ in range(h)]
    dist = [[h * w * 100 for i in range(w)] for j in range(h)]
    dist[0][0] = 1
    q = queue.Queue()
    q.put((0, 0))
    while not q.empty():
        (x, y) = q.get()
        nd = dist[x][y] + 1
        for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                continue
            if field[nx][ny] == "." and dist[nx][ny] > nd:
                dist[nx][ny] = nd
                q.put((nx, ny))
    if dist[h - 1][w - 1] >= h * w:
        return -1
    ans = h * w - dist[h - 1][w - 1]
    for v in field:
        ans -= v.count("#")
    return ans


if __name__ == "__main__":
    print((solve()))

-    dx = [-1, 1, 0, 0] -    dy = [0, 0, 1, -1] +        nd = dist[x][y] + 1 -            if field[nx][ny] == "#": -                continue -            if dist[nx][ny] > dist[x][y] + 1: -                dist[nx][ny] = dist[x][y] + 1 +            if field[nx][ny] == "." and dist[nx][ny] > nd: +                dist[nx][ny] = nd -    # print(dist) -        for c in v: -            if c == "#": -                ans -= 1 +        ans -= v.count("#")