# -*- coding: utf-8 -*-



import sys

input = sys.stdin.readline



from collections import deque



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



start, goal = (1, 1), (H, W)

# 左右上下

directions = [[0,-1],[0,1],[-1,0],[1,0]]

# 四方に一回り大きいフィールドを作る

field = [['*'] * (W+2) for i in range(H+2)]

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

    row = list(eval(input()))[:-1]

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

        field[i][j] = row[j-1]



# 移動距離メモ

memo = [[float('inf')] * (W+2) for i in range(H+2)]

memo[start[0]][start[1]] = 0

# 訪問済メモ

visited = [[0] * (W+2) for i in range(H+2)]

# 移動元メモ

prevs = [[None] * (W+2) for i in range(H+2)]



def bfs():

    que = deque()

    # スタート位置

    que.append(start)

    while len(que) != 0:

        cur = que.popleft()

        # 見終わった場所はもうやらない

        if visited[cur[0]][cur[1]] == 1:

            continue

        # 訪問済にする

        visited[cur[0]][cur[1]] = 1

        # 4方向見る

        for direction in directions:

            # 上下左右に1つずつ動かした座標

            nxt = list(map(lambda x,y: x+y, cur, direction))

            # 壁か黒マスはスキップ

            if field[nxt[0]][nxt[1]] == '*' or field[nxt[0]][nxt[1]] == '#':

                continue

            # 現在位置+1の距離を移動先に記録

            if field[nxt[0]][nxt[1]] == '.' and memo[nxt[0]][nxt[1]] > memo[cur[0]][cur[1]] + 1:

                memo[nxt[0]][nxt[1]] = memo[cur[0]][cur[1]] + 1

                # ここで移動元のマスを記録

                prevs[nxt[0]][nxt[1]] = (cur[0], cur[1])

                # キューに次のマスを足す

                que.append(nxt)



# 最短経路を出す

bfs()

# ゴールに辿り着けない場合

if memo[goal[0]][goal[1]] == float('inf'):

    print((-1))

    exit()

# 最短経路で使うマスに目印をつける

field[goal[0]][goal[1]] = '@'

nxt = prevs[goal[0]][goal[1]]

for _ in range(memo[goal[0]][goal[1]]):

    field[nxt[0]][nxt[1]] = '@'

    nxt = prevs[nxt[0]][nxt[1]]

# 残っている白マスが塗り替えられる場所

ans = 0

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

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

        if field[i][j] == '.':

            ans += 1

print(ans)

# -*- coding: utf-8 -*-



import sys

from collections import deque



def input(): return sys.stdin.readline().strip()

def list2d(a, b, c): return [[c] * b for i in range(a)]

def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1): return int(-(-x // y))

def INT(): return int(eval(input()))

def MAP(): return list(map(int, input().split()))

def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes(): print('Yes')

def No(): print('No')

def YES(): print('YES')

def NO(): print('NO')

sys.setrecursionlimit(10 ** 9)

INF = 10 ** 18

MOD = 10 ** 9 + 7



def build_grid(H, W, intv, _type, space=True, padding=False):

    # 入力がスペース区切りかどうか

    if space:

        _input = lambda: input().split()

    else:

        _input = lambda: eval(input())

    _list = lambda: list(map(_type, _input()))

    # 余白の有無

    if padding:

        offset = 1

    else:

        offset = 0

    grid = list2d(H+offset*2, W+offset*2, intv)

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

        row = _list()

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

            grid[i][j] = row[j-offset]

    return grid



H, W = MAP()

grid = build_grid(H, W, '#', str, space=False, padding=True)



# グリッドBFS

directions = ((1,0),(-1,0),(0,1),(0,-1))

que = deque([(1, 1, 0)])

dist = list2d(H+2, W+2, INF)

while que:

    h, w, c = que.popleft()

    if dist[h][w] != INF:

        continue

    dist[h][w] = c

    for dh, dw in directions:

        h2 = h + dh

        w2 = w + dw

        if grid[h2][w2] == '#':

            continue

        que.append((h2, w2, c+1))



# ゴール到達不可

if dist[H][W] == INF:

    print((-1))

    exit()



# 経路復元

que = deque([(H, W)])

while que:

    h, w = que.popleft()

    grid[h][w] = '@'

    for dh, dw in directions:

        h2 = h + dh

        w2 = w + dw

        if dist[h2][w2] == dist[h][w] - 1:

            que.append((h2, w2))

            break



# 最短経路で通る場所以外をカウント

ans = 0

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

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

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

            ans += 1

print(ans)

# -*- coding: utf-8 -*-
import sys

input = sys.stdin.readline
from collections import deque

H, W = list(map(int, input().split()))
start, goal = (1, 1), (H, W)
# 左右上下
directions = [[0, -1], [0, 1], [-1, 0], [1, 0]]
# 四方に一回り大きいフィールドを作る
field = [["*"] * (W + 2) for i in range(H + 2)]
for i in range(1, H + 1):
    row = list(eval(input()))[:-1]
    for j in range(1, W + 1):
        field[i][j] = row[j - 1]
# 移動距離メモ
memo = [[float("inf")] * (W + 2) for i in range(H + 2)]
memo[start[0]][start[1]] = 0
# 訪問済メモ
visited = [[0] * (W + 2) for i in range(H + 2)]
# 移動元メモ
prevs = [[None] * (W + 2) for i in range(H + 2)]


def bfs():
    que = deque()
    # スタート位置
    que.append(start)
    while len(que) != 0:
        cur = que.popleft()
        # 見終わった場所はもうやらない
        if visited[cur[0]][cur[1]] == 1:
            continue
        # 訪問済にする
        visited[cur[0]][cur[1]] = 1
        # 4方向見る
        for direction in directions:
            # 上下左右に1つずつ動かした座標
            nxt = list(map(lambda x, y: x + y, cur, direction))
            # 壁か黒マスはスキップ
            if field[nxt[0]][nxt[1]] == "*" or field[nxt[0]][nxt[1]] == "#":
                continue
            # 現在位置+1の距離を移動先に記録
            if (
                field[nxt[0]][nxt[1]] == "."
                and memo[nxt[0]][nxt[1]] > memo[cur[0]][cur[1]] + 1
            ):
                memo[nxt[0]][nxt[1]] = memo[cur[0]][cur[1]] + 1
                # ここで移動元のマスを記録
                prevs[nxt[0]][nxt[1]] = (cur[0], cur[1])
                # キューに次のマスを足す
                que.append(nxt)


# 最短経路を出す
bfs()
# ゴールに辿り着けない場合
if memo[goal[0]][goal[1]] == float("inf"):
    print((-1))
    exit()
# 最短経路で使うマスに目印をつける
field[goal[0]][goal[1]] = "@"
nxt = prevs[goal[0]][goal[1]]
for _ in range(memo[goal[0]][goal[1]]):
    field[nxt[0]][nxt[1]] = "@"
    nxt = prevs[nxt[0]][nxt[1]]
# 残っている白マスが塗り替えられる場所
ans = 0
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if field[i][j] == ".":
            ans += 1
print(ans)

# -*- coding: utf-8 -*-
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


def list2d(a, b, c):
    return [[c] * b for i in range(a)]


def list3d(a, b, c, d):
    return [[[d] * c for j in range(b)] for i in range(a)]


def list4d(a, b, c, d, e):
    return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]


def ceil(x, y=1):
    return int(-(-x // y))


def INT():
    return int(eval(input()))


def MAP():
    return list(map(int, input().split()))


def LIST(N=None):
    return list(MAP()) if N is None else [INT() for i in range(N)]


def Yes():
    print("Yes")


def No():
    print("No")


def YES():
    print("YES")


def NO():
    print("NO")


sys.setrecursionlimit(10**9)
INF = 10**18
MOD = 10**9 + 7


def build_grid(H, W, intv, _type, space=True, padding=False):
    # 入力がスペース区切りかどうか
    if space:
        _input = lambda: input().split()
    else:
        _input = lambda: eval(input())
    _list = lambda: list(map(_type, _input()))
    # 余白の有無
    if padding:
        offset = 1
    else:
        offset = 0
    grid = list2d(H + offset * 2, W + offset * 2, intv)
    for i in range(offset, H + offset):
        row = _list()
        for j in range(offset, W + offset):
            grid[i][j] = row[j - offset]
    return grid


H, W = MAP()
grid = build_grid(H, W, "#", str, space=False, padding=True)
# グリッドBFS
directions = ((1, 0), (-1, 0), (0, 1), (0, -1))
que = deque([(1, 1, 0)])
dist = list2d(H + 2, W + 2, INF)
while que:
    h, w, c = que.popleft()
    if dist[h][w] != INF:
        continue
    dist[h][w] = c
    for dh, dw in directions:
        h2 = h + dh
        w2 = w + dw
        if grid[h2][w2] == "#":
            continue
        que.append((h2, w2, c + 1))
# ゴール到達不可
if dist[H][W] == INF:
    print((-1))
    exit()
# 経路復元
que = deque([(H, W)])
while que:
    h, w = que.popleft()
    grid[h][w] = "@"
    for dh, dw in directions:
        h2 = h + dh
        w2 = w + dw
        if dist[h2][w2] == dist[h][w] - 1:
            que.append((h2, w2))
            break
# 最短経路で通る場所以外をカウント
ans = 0
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if grid[i][j] == ".":
            ans += 1
print(ans)

- -input = sys.stdin.readline -H, W = list(map(int, input().split())) -start, goal = (1, 1), (H, W) -# 左右上下 -directions = [[0, -1], [0, 1], [-1, 0], [1, 0]] -# 四方に一回り大きいフィールドを作る -field = [["*"] * (W + 2) for i in range(H + 2)] -for i in range(1, H + 1): -    row = list(eval(input()))[:-1] -    for j in range(1, W + 1): -        field[i][j] = row[j - 1] -# 移動距離メモ -memo = [[float("inf")] * (W + 2) for i in range(H + 2)] -memo[start[0]][start[1]] = 0 -# 訪問済メモ -visited = [[0] * (W + 2) for i in range(H + 2)] -# 移動元メモ -prevs = [[None] * (W + 2) for i in range(H + 2)] + +def input(): +    return sys.stdin.readline().strip() -def bfs(): -    que = deque() -    # スタート位置 -    que.append(start) -    while len(que) != 0: -        cur = que.popleft() -        # 見終わった場所はもうやらない -        if visited[cur[0]][cur[1]] == 1: -            continue -        # 訪問済にする -        visited[cur[0]][cur[1]] = 1 -        # 4方向見る -        for direction in directions: -            # 上下左右に1つずつ動かした座標 -            nxt = list(map(lambda x, y: x + y, cur, direction)) -            # 壁か黒マスはスキップ -            if field[nxt[0]][nxt[1]] == "*" or field[nxt[0]][nxt[1]] == "#": -                continue -            # 現在位置+1の距離を移動先に記録 -            if ( -                field[nxt[0]][nxt[1]] == "." -                and memo[nxt[0]][nxt[1]] > memo[cur[0]][cur[1]] + 1 -            ): -                memo[nxt[0]][nxt[1]] = memo[cur[0]][cur[1]] + 1 -                # ここで移動元のマスを記録 -                prevs[nxt[0]][nxt[1]] = (cur[0], cur[1]) -                # キューに次のマスを足す -                que.append(nxt) +def list2d(a, b, c): +    return [[c] * b for i in range(a)] -# 最短経路を出す -bfs() -# ゴールに辿り着けない場合 -if memo[goal[0]][goal[1]] == float("inf"): +def list3d(a, b, c, d): +    return [[[d] * c for j in range(b)] for i in range(a)] + + +def list4d(a, b, c, d, e): +    return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] + + +def ceil(x, y=1): +    return int(-(-x // y)) + + +def INT(): +    return int(eval(input())) + + +def MAP(): +    return list(map(int, input().split())) + + +def LIST(N=None): +    return list(MAP()) if N is None else [INT() for i in range(N)] + + +def Yes(): +    print("Yes") + + +def No(): +    print("No") + + +def YES(): +    print("YES") + + +def NO(): +    print("NO") + + +sys.setrecursionlimit(10**9) +INF = 10**18 +MOD = 10**9 + 7 + + +def build_grid(H, W, intv, _type, space=True, padding=False): +    # 入力がスペース区切りかどうか +    if space: +        _input = lambda: input().split() +    else: +        _input = lambda: eval(input()) +    _list = lambda: list(map(_type, _input())) +    # 余白の有無 +    if padding: +        offset = 1 +    else: +        offset = 0 +    grid = list2d(H + offset * 2, W + offset * 2, intv) +    for i in range(offset, H + offset): +        row = _list() +        for j in range(offset, W + offset): +            grid[i][j] = row[j - offset] +    return grid + + +H, W = MAP() +grid = build_grid(H, W, "#", str, space=False, padding=True) +# グリッドBFS +directions = ((1, 0), (-1, 0), (0, 1), (0, -1)) +que = deque([(1, 1, 0)]) +dist = list2d(H + 2, W + 2, INF) +while que: +    h, w, c = que.popleft() +    if dist[h][w] != INF: +        continue +    dist[h][w] = c +    for dh, dw in directions: +        h2 = h + dh +        w2 = w + dw +        if grid[h2][w2] == "#": +            continue +        que.append((h2, w2, c + 1)) +# ゴール到達不可 +if dist[H][W] == INF: -# 最短経路で使うマスに目印をつける -field[goal[0]][goal[1]] = "@" -nxt = prevs[goal[0]][goal[1]] -for _ in range(memo[goal[0]][goal[1]]): -    field[nxt[0]][nxt[1]] = "@" -    nxt = prevs[nxt[0]][nxt[1]] -# 残っている白マスが塗り替えられる場所 +# 経路復元 +que = deque([(H, W)]) +while que: +    h, w = que.popleft() +    grid[h][w] = "@" +    for dh, dw in directions: +        h2 = h + dh +        w2 = w + dw +        if dist[h2][w2] == dist[h][w] - 1: +            que.append((h2, w2)) +            break +# 最短経路で通る場所以外をカウント -        if field[i][j] == ".": +        if grid[i][j] == ".":