from collections import deque



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

grid = []

for i in range(h):

    grid.append([j for j in eval(input())])



parent = [[-1 for j in range(w)] for i in range(h)]





def bfs(s):

    q = deque()

    check = [[0 for j in range(w)] for i in range(h)]



    q.append(s)

    check[s[0]][s[1]] = 1

    while len(q) != 0:

        u = q.popleft()

        for i in range(h):

            for j in range(w):

                if grid[i][j] == '.' and check[i][j] == 0:

                    dx, dy = abs(i-u[0]), abs(j-u[1])

                    if dx <= 1 and dy <= 1 and dx + dy < 2:

                        check[i][j] = 1

                        parent[i][j] = u

                        q.append((i, j))

        check[u[0]][u[1]] = 2





def main():

    bfs((0, 0))



    if parent[h-1][w-1] != -1:

        x, y = h-1, w-1

        while x != 0 or y != 0:

            grid[x][y] = "#"

            x, y = parent[x][y]

        grid[0][0] = "#"



        pt = 0

        for i in range(h):

            for j in range(w):

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

                    pt += 1



    else:

        pt = -1



    print(pt)





if __name__ == '__main__':

    main()

from collections import deque



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

grid = []

for i in range(h):

    grid.append([j for j in eval(input())])



parent = [[-1 for j in range(w)] for i in range(h)]





def bfs(s):

    q = deque()

    check = [[0 for j in range(w)] for i in range(h)]



    q.append(s)

    check[s[0]][s[1]] = 1

    while len(q) != 0:

        u = q.popleft()

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

        if x > 0 and grid[x-1][y] == "." and check[x-1][y] == 0:

            check[x-1][y], parent[x-1][y] = 1, u

            q.append((x-1, y))

        if y > 0 and grid[x][y-1] == "." and check[x][y-1] == 0:

            check[x][y-1], parent[x][y-1] = 1, u

            q.append((x, y-1))

        if x < h-1 and grid[x+1][y] == "." and check[x+1][y] == 0:

            check[x+1][y], parent[x+1][y] = 1, u

            q.append((x+1, y))

        if y < w-1 and grid[x][y+1] == "." and check[x][y+1] == 0:

            check[x][y+1], parent[x][y+1] = 1, u

            q.append((x, y+1))



        check[x][y] = 2





def main():

    bfs((0, 0))



    if parent[h-1][w-1] != -1:

        x, y = h-1, w-1

        while x != 0 or y != 0:

            grid[x][y] = "#"

            x, y = parent[x][y]

        grid[0][0] = "#"



        pt = 0

        for i in range(h):

            for j in range(w):

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

                    pt += 1



    else:

        pt = -1



    print(pt)





if __name__ == '__main__':

    main()

from collections import deque

h, w = list(map(int, input().split()))
grid = []
for i in range(h):
    grid.append([j for j in eval(input())])
parent = [[-1 for j in range(w)] for i in range(h)]


def bfs(s):
    q = deque()
    check = [[0 for j in range(w)] for i in range(h)]
    q.append(s)
    check[s[0]][s[1]] = 1
    while len(q) != 0:
        u = q.popleft()
        for i in range(h):
            for j in range(w):
                if grid[i][j] == "." and check[i][j] == 0:
                    dx, dy = abs(i - u[0]), abs(j - u[1])
                    if dx <= 1 and dy <= 1 and dx + dy < 2:
                        check[i][j] = 1
                        parent[i][j] = u
                        q.append((i, j))
        check[u[0]][u[1]] = 2


def main():
    bfs((0, 0))
    if parent[h - 1][w - 1] != -1:
        x, y = h - 1, w - 1
        while x != 0 or y != 0:
            grid[x][y] = "#"
            x, y = parent[x][y]
        grid[0][0] = "#"
        pt = 0
        for i in range(h):
            for j in range(w):
                if grid[i][j] == ".":
                    pt += 1
    else:
        pt = -1
    print(pt)


if __name__ == "__main__":
    main()

from collections import deque

h, w = list(map(int, input().split()))
grid = []
for i in range(h):
    grid.append([j for j in eval(input())])
parent = [[-1 for j in range(w)] for i in range(h)]


def bfs(s):
    q = deque()
    check = [[0 for j in range(w)] for i in range(h)]
    q.append(s)
    check[s[0]][s[1]] = 1
    while len(q) != 0:
        u = q.popleft()
        x, y = u[0], u[1]
        if x > 0 and grid[x - 1][y] == "." and check[x - 1][y] == 0:
            check[x - 1][y], parent[x - 1][y] = 1, u
            q.append((x - 1, y))
        if y > 0 and grid[x][y - 1] == "." and check[x][y - 1] == 0:
            check[x][y - 1], parent[x][y - 1] = 1, u
            q.append((x, y - 1))
        if x < h - 1 and grid[x + 1][y] == "." and check[x + 1][y] == 0:
            check[x + 1][y], parent[x + 1][y] = 1, u
            q.append((x + 1, y))
        if y < w - 1 and grid[x][y + 1] == "." and check[x][y + 1] == 0:
            check[x][y + 1], parent[x][y + 1] = 1, u
            q.append((x, y + 1))
        check[x][y] = 2


def main():
    bfs((0, 0))
    if parent[h - 1][w - 1] != -1:
        x, y = h - 1, w - 1
        while x != 0 or y != 0:
            grid[x][y] = "#"
            x, y = parent[x][y]
        grid[0][0] = "#"
        pt = 0
        for i in range(h):
            for j in range(w):
                if grid[i][j] == ".":
                    pt += 1
    else:
        pt = -1
    print(pt)


if __name__ == "__main__":
    main()

-        for i in range(h): -            for j in range(w): -                if grid[i][j] == "." and check[i][j] == 0: -                    dx, dy = abs(i - u[0]), abs(j - u[1]) -                    if dx <= 1 and dy <= 1 and dx + dy < 2: -                        check[i][j] = 1 -                        parent[i][j] = u -                        q.append((i, j)) -        check[u[0]][u[1]] = 2 +        x, y = u[0], u[1] +        if x > 0 and grid[x - 1][y] == "." and check[x - 1][y] == 0: +            check[x - 1][y], parent[x - 1][y] = 1, u +            q.append((x - 1, y)) +        if y > 0 and grid[x][y - 1] == "." and check[x][y - 1] == 0: +            check[x][y - 1], parent[x][y - 1] = 1, u +            q.append((x, y - 1)) +        if x < h - 1 and grid[x + 1][y] == "." and check[x + 1][y] == 0: +            check[x + 1][y], parent[x + 1][y] = 1, u +            q.append((x + 1, y)) +        if y < w - 1 and grid[x][y + 1] == "." and check[x][y + 1] == 0: +            check[x][y + 1], parent[x][y + 1] = 1, u +            q.append((x, y + 1)) +        check[x][y] = 2