import sys

import numpy as np

from heapq import heappop, heappush



# Dijikstra

def main():

    H, W = list(map(int, sys.stdin.readline().split()))



    grid = np.array([list(sys.stdin.readline().rstrip()) for _ in range(H)])

    grid = np.pad(grid, 1, 'constant')

    # atcoderではnp.pad() のargにconstant_valuesを指定できない

    q = []

    heappush(q, (0, 1, 1))

    visited = set()

    cost = np.full((H + 1, W + 1), np.inf)

    cost[1][1] = 0

    while q:

        c, i, j = heappop(q)

        if (i, j) in visited:

            continue

        visited.add((i, j))

        cost[i][j] = c

        if i == H and j == W:

            break

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

            y = i + dy

            x = j + dx

            if grid[y][x] == '.' and not (y, x) in visited:

                heappush(q, (c + 1, y, x))

    



    if cost[H][W] != np.inf:

        ans = int(np.sum(grid == '.') - (cost[H][W] + 1))

    else:

        ans = -1



    print(ans)  



if __name__ == "__main__":

    main()
import sys

from heapq import heappop, heappush



H, W = list(map(int, sys.stdin.readline().split()))

grid = ['#' * (W + 2)]

grid += ['#' + sys.stdin.readline().rstrip() + '#' for _ in range(H)]

grid += ['#' * (W + 2)]



def heuristic_cost(y, x):

    return abs(H - y) + abs(W - x)



def main():

    h = heuristic_cost(1, 1)

    c = 0

    s = h + c

    hq = [(s, c, 1, 1)]

    nex = [(1, 0), (0, 1), (-1, 0), (0, -1)]

    cost = [[None] * (W+1) for _ in range(H+1)]

    while hq:

        s, c, y, x = heappop(hq)

        if not cost[y][x] is None:

            continue

        cost[y][x] = c

        for dy, dx in nex:

            i = y + dy

            j = x + dx

            if grid[i][j] == '.' and cost[i][j] is None:

                h = heuristic_cost(i, j)

                s = h + (c + 1)

                heappush(hq, (s, c+1, i, j))



    white_cnt = 0

    for i in range(1, H+1):

        for j in range(1, W+1):

            white_cnt += (grid[i][j] == '.') & 1

    

    ans = white_cnt - (cost[H][W] + 1) if not cost[H][W] is None else -1

    return ans 



if __name__ == '__main__':

    ans = main()

    print(ans)
import sys
import numpy as np
from heapq import heappop, heappush

# Dijikstra
def main():
    H, W = list(map(int, sys.stdin.readline().split()))
    grid = np.array([list(sys.stdin.readline().rstrip()) for _ in range(H)])
    grid = np.pad(grid, 1, "constant")
    # atcoderではnp.pad() のargにconstant_valuesを指定できない
    q = []
    heappush(q, (0, 1, 1))
    visited = set()
    cost = np.full((H + 1, W + 1), np.inf)
    cost[1][1] = 0
    while q:
        c, i, j = heappop(q)
        if (i, j) in visited:
            continue
        visited.add((i, j))
        cost[i][j] = c
        if i == H and j == W:
            break
        for dy, dx in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
            y = i + dy
            x = j + dx
            if grid[y][x] == "." and not (y, x) in visited:
                heappush(q, (c + 1, y, x))
    if cost[H][W] != np.inf:
        ans = int(np.sum(grid == ".") - (cost[H][W] + 1))
    else:
        ans = -1
    print(ans)


if __name__ == "__main__":
    main()

import sys
from heapq import heappop, heappush

H, W = list(map(int, sys.stdin.readline().split()))
grid = ["#" * (W + 2)]
grid += ["#" + sys.stdin.readline().rstrip() + "#" for _ in range(H)]
grid += ["#" * (W + 2)]


def heuristic_cost(y, x):
    return abs(H - y) + abs(W - x)


def main():
    h = heuristic_cost(1, 1)
    c = 0
    s = h + c
    hq = [(s, c, 1, 1)]
    nex = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    cost = [[None] * (W + 1) for _ in range(H + 1)]
    while hq:
        s, c, y, x = heappop(hq)
        if not cost[y][x] is None:
            continue
        cost[y][x] = c
        for dy, dx in nex:
            i = y + dy
            j = x + dx
            if grid[i][j] == "." and cost[i][j] is None:
                h = heuristic_cost(i, j)
                s = h + (c + 1)
                heappush(hq, (s, c + 1, i, j))
    white_cnt = 0
    for i in range(1, H + 1):
        for j in range(1, W + 1):
            white_cnt += (grid[i][j] == ".") & 1
    ans = white_cnt - (cost[H][W] + 1) if not cost[H][W] is None else -1
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

-import numpy as np -# Dijikstra +H, W = list(map(int, sys.stdin.readline().split())) +grid = ["#" * (W + 2)] +grid += ["#" + sys.stdin.readline().rstrip() + "#" for _ in range(H)] +grid += ["#" * (W + 2)] + + +def heuristic_cost(y, x): +    return abs(H - y) + abs(W - x) + + -    H, W = list(map(int, sys.stdin.readline().split())) -    grid = np.array([list(sys.stdin.readline().rstrip()) for _ in range(H)]) -    grid = np.pad(grid, 1, "constant") -    # atcoderではnp.pad() のargにconstant_valuesを指定できない -    q = [] -    heappush(q, (0, 1, 1)) -    visited = set() -    cost = np.full((H + 1, W + 1), np.inf) -    cost[1][1] = 0 -    while q: -        c, i, j = heappop(q) -        if (i, j) in visited: +    h = heuristic_cost(1, 1) +    c = 0 +    s = h + c +    hq = [(s, c, 1, 1)] +    nex = [(1, 0), (0, 1), (-1, 0), (0, -1)] +    cost = [[None] * (W + 1) for _ in range(H + 1)] +    while hq: +        s, c, y, x = heappop(hq) +        if not cost[y][x] is None: -        visited.add((i, j)) -        cost[i][j] = c -        if i == H and j == W: -            break -        for dy, dx in [(1, 0), (0, 1), (-1, 0), (0, -1)]: -            y = i + dy -            x = j + dx -            if grid[y][x] == "." and not (y, x) in visited: -                heappush(q, (c + 1, y, x)) -    if cost[H][W] != np.inf: -        ans = int(np.sum(grid == ".") - (cost[H][W] + 1)) -    else: -        ans = -1 -    print(ans) +        cost[y][x] = c +        for dy, dx in nex: +            i = y + dy +            j = x + dx +            if grid[i][j] == "." and cost[i][j] is None: +                h = heuristic_cost(i, j) +                s = h + (c + 1) +                heappush(hq, (s, c + 1, i, j)) +    white_cnt = 0 +    for i in range(1, H + 1): +        for j in range(1, W + 1): +            white_cnt += (grid[i][j] == ".") & 1 +    ans = white_cnt - (cost[H][W] + 1) if not cost[H][W] is None else -1 +    return ans -    main() +    ans = main() +    print(ans)