from collections import deque

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

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

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

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



sx, sy, gx, gy = 0, 0, h - 1, w - 1

d = [[float("inf")] * w for _ in range(h)]



que = deque([])

que.append((sx, sy))

d[sx][sy] = 0





def bfs():

    while que:

        p = que.popleft()

        if p[0] == gx and p[1] == gy:

            break

        for i in range(4):

            nx = p[0] + dx[i]

            ny = p[1] + dy[i]

            if 0 <= nx < h and 0 <= ny < w and maze[nx][ny] != "#" and d[nx][ny] == float("inf"):

                que.append((nx, ny))

                d[nx][ny] = d[p[0]][p[1]] + 1

    return d[gx][gy]



##最短距離を格納

bfs = bfs()

cnt = 0

for i in range(h):

    for j in range(w):

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

            cnt += 1

if bfs == float("inf"):

    print((-1))

else:

    print((cnt - (bfs + 1)))

from collections import deque

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

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

dh = [1, 0, -1, 0]

dw = [0, 1, 0, -1]



sh, sw, gh, gw = 0, 0, h - 1, w - 1

d = [[float("inf")] * w for _ in range(h)]



que = deque([])

que.append((sh, sw))

d[sh][sw] = 0





def bfs():

    while que:

        p = que.popleft()

        if p[0] == gh and p[1] == gw:

            break

        for i in range(4):

            nh = p[0] + dh[i]

            nw = p[1] + dw[i]

            if 0 <= nh < h and 0 <= nw < w and maze[nh][nw] != "#" and d[nh][nw] == float("inf"):

                que.append((nh, nw))

                d[nh][nw] = d[p[0]][p[1]] + 1

    return d[gh][gw]



##最短距離を格納

bfs = bfs()

cnt = 0

for i in range(h):

    for j in range(w):

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

            cnt += 1

if bfs == float("inf"):

    print((-1))

else:

    print((cnt - (bfs + 1)))

from collections import deque

h, w = list(map(int, input().split()))
maze = [list(eval(input())) for i in range(h)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
sx, sy, gx, gy = 0, 0, h - 1, w - 1
d = [[float("inf")] * w for _ in range(h)]
que = deque([])
que.append((sx, sy))
d[sx][sy] = 0


def bfs():
    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (
                0 <= nx < h
                and 0 <= ny < w
                and maze[nx][ny] != "#"
                and d[nx][ny] == float("inf")
            ):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
    return d[gx][gy]


##最短距離を格納
bfs = bfs()
cnt = 0
for i in range(h):
    for j in range(w):
        if maze[i][j] == ".":
            cnt += 1
if bfs == float("inf"):
    print((-1))
else:
    print((cnt - (bfs + 1)))

from collections import deque

h, w = list(map(int, input().split()))
maze = [list(eval(input())) for i in range(h)]
dh = [1, 0, -1, 0]
dw = [0, 1, 0, -1]
sh, sw, gh, gw = 0, 0, h - 1, w - 1
d = [[float("inf")] * w for _ in range(h)]
que = deque([])
que.append((sh, sw))
d[sh][sw] = 0


def bfs():
    while que:
        p = que.popleft()
        if p[0] == gh and p[1] == gw:
            break
        for i in range(4):
            nh = p[0] + dh[i]
            nw = p[1] + dw[i]
            if (
                0 <= nh < h
                and 0 <= nw < w
                and maze[nh][nw] != "#"
                and d[nh][nw] == float("inf")
            ):
                que.append((nh, nw))
                d[nh][nw] = d[p[0]][p[1]] + 1
    return d[gh][gw]


##最短距離を格納
bfs = bfs()
cnt = 0
for i in range(h):
    for j in range(w):
        if maze[i][j] == ".":
            cnt += 1
if bfs == float("inf"):
    print((-1))
else:
    print((cnt - (bfs + 1)))

-dx = [1, 0, -1, 0] -dy = [0, 1, 0, -1] -sx, sy, gx, gy = 0, 0, h - 1, w - 1 +dh = [1, 0, -1, 0] +dw = [0, 1, 0, -1] +sh, sw, gh, gw = 0, 0, h - 1, w - 1 -que.append((sx, sy)) -d[sx][sy] = 0 +que.append((sh, sw)) +d[sh][sw] = 0 -        if p[0] == gx and p[1] == gy: +        if p[0] == gh and p[1] == gw: -            nx = p[0] + dx[i] -            ny = p[1] + dy[i] +            nh = p[0] + dh[i] +            nw = p[1] + dw[i] -                0 <= nx < h -                and 0 <= ny < w -                and maze[nx][ny] != "#" -                and d[nx][ny] == float("inf") +                0 <= nh < h +                and 0 <= nw < w +                and maze[nh][nw] != "#" +                and d[nh][nw] == float("inf") -                que.append((nx, ny)) -                d[nx][ny] = d[p[0]][p[1]] + 1 -    return d[gx][gy] +                que.append((nh, nw)) +                d[nh][nw] = d[p[0]][p[1]] + 1 +    return d[gh][gw]