from collections import deque



INF = float('inf')



dr = (-1, 0, 0, 1)

dc = (0, -1, 1, 0)



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



s = []

total_white_count = 0

for _ in range(H):

    t = eval(input())

    s.append(t)

    total_white_count += t.count('.')

p = [[INF for _ in range(W)] for _ in range(H)]



ans = -1



dq = deque()

dq.append((0, 0, 1))

while dq:

    r, c, d = dq.popleft() # 直した



    nd = d + 1

    for i in range(4):

        nr = r + dr[i]

        nc = c + dc[i]

        if not (0 <= nr < H and 0 <= nc < W): continue

        if s[nr][nc] == '#' : continue

        if p[nr][nc] <= nd: continue

        p[nr][nc] = nd

        if nr == H - 1 and nc == W - 1:

            ans = total_white_count - nd

            break

        dq.append((nr, nc, nd))

    else:

        continue

    break



print(ans)

def main():

    from collections import deque

    import sys



    input = sys.stdin.readline



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

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



    bk = 0

    for row in s:

        bk += row.count('#')



    def coods(r, c):

        if r > 0: yield r - 1, c

        if r < h - 1: yield r + 1, c

        if c > 0: yield r, c - 1

        if c < w - 1: yield r, c + 1



    q = deque()

    q.append((0, 0))

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

    count[0][0] = 1

    while q:

        r, c = q.popleft()

        k = count[r][c]

        for nr, nc in coods(r, c):

            nchar = s[nr][nc]

            if nchar == '#': continue

            if count[nr][nc] != -1: continue

            count[nr][nc] = k + 1

            if (nr, nc) == (h - 1, w - 1):

                print((h * w - (k + 1) - bk))

                return

            q.append((nr, nc))

    print((-1))





if __name__ == '__main__':

    main()

from collections import deque

INF = float("inf")
dr = (-1, 0, 0, 1)
dc = (0, -1, 1, 0)
H, W = list(map(int, input().split()))
s = []
total_white_count = 0
for _ in range(H):
    t = eval(input())
    s.append(t)
    total_white_count += t.count(".")
p = [[INF for _ in range(W)] for _ in range(H)]
ans = -1
dq = deque()
dq.append((0, 0, 1))
while dq:
    r, c, d = dq.popleft()  # 直した
    nd = d + 1
    for i in range(4):
        nr = r + dr[i]
        nc = c + dc[i]
        if not (0 <= nr < H and 0 <= nc < W):
            continue
        if s[nr][nc] == "#":
            continue
        if p[nr][nc] <= nd:
            continue
        p[nr][nc] = nd
        if nr == H - 1 and nc == W - 1:
            ans = total_white_count - nd
            break
        dq.append((nr, nc, nd))
    else:
        continue
    break
print(ans)

def main():
    from collections import deque
    import sys

    input = sys.stdin.readline
    h, w = list(map(int, input().split()))
    s = [eval(input()) for _ in range(h)]
    bk = 0
    for row in s:
        bk += row.count("#")

    def coods(r, c):
        if r > 0:
            yield r - 1, c
        if r < h - 1:
            yield r + 1, c
        if c > 0:
            yield r, c - 1
        if c < w - 1:
            yield r, c + 1

    q = deque()
    q.append((0, 0))
    count = [[-1] * w for _ in range(h)]
    count[0][0] = 1
    while q:
        r, c = q.popleft()
        k = count[r][c]
        for nr, nc in coods(r, c):
            nchar = s[nr][nc]
            if nchar == "#":
                continue
            if count[nr][nc] != -1:
                continue
            count[nr][nc] = k + 1
            if (nr, nc) == (h - 1, w - 1):
                print((h * w - (k + 1) - bk))
                return
            q.append((nr, nc))
    print((-1))


if __name__ == "__main__":
    main()

-from collections import deque +def main(): +    from collections import deque +    import sys -INF = float("inf") -dr = (-1, 0, 0, 1) -dc = (0, -1, 1, 0) -H, W = list(map(int, input().split())) -s = [] -total_white_count = 0 -for _ in range(H): -    t = eval(input()) -    s.append(t) -    total_white_count += t.count(".") -p = [[INF for _ in range(W)] for _ in range(H)] -ans = -1 -dq = deque() -dq.append((0, 0, 1)) -while dq: -    r, c, d = dq.popleft()  # 直した -    nd = d + 1 -    for i in range(4): -        nr = r + dr[i] -        nc = c + dc[i] -        if not (0 <= nr < H and 0 <= nc < W): -            continue -        if s[nr][nc] == "#": -            continue -        if p[nr][nc] <= nd: -            continue -        p[nr][nc] = nd -        if nr == H - 1 and nc == W - 1: -            ans = total_white_count - nd -            break -        dq.append((nr, nc, nd)) -    else: -        continue -    break -print(ans) +    input = sys.stdin.readline +    h, w = list(map(int, input().split())) +    s = [eval(input()) for _ in range(h)] +    bk = 0 +    for row in s: +        bk += row.count("#") + +    def coods(r, c): +        if r > 0: +            yield r - 1, c +        if r < h - 1: +            yield r + 1, c +        if c > 0: +            yield r, c - 1 +        if c < w - 1: +            yield r, c + 1 + +    q = deque() +    q.append((0, 0)) +    count = [[-1] * w for _ in range(h)] +    count[0][0] = 1 +    while q: +        r, c = q.popleft() +        k = count[r][c] +        for nr, nc in coods(r, c): +            nchar = s[nr][nc] +            if nchar == "#": +                continue +            if count[nr][nc] != -1: +                continue +            count[nr][nc] = k + 1 +            if (nr, nc) == (h - 1, w - 1): +                print((h * w - (k + 1) - bk)) +                return +            q.append((nr, nc)) +    print((-1)) + + +if __name__ == "__main__": +    main()