from collections import deque



def bfs():

    flag = False

    d = [[float('inf')] * W for i in range(H)]

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

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



    que = deque([])

    que.append((0, 0))

    d[0][0] = 0



    while que:

        p = que.popleft()

        if p[0]==H-1 and p[1]==W-1:

            flag = True

            break



        for i in range(4):

            nx = p[1] + dx[i]

            ny = p[0] + dy[i]



            if 0<=nx<W and 0<=ny<H and maze[ny][nx]!='#' and d[ny][nx]==float("inf"):

                que.append((ny, nx))

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



    if flag==False:

        return False



    return d[H-1][W-1]



def notcount(H, W, maze):

    cnt = 0



    for i in range(H):

        for j in range(W):

            if maze[i][j]=='#':

                cnt += 1



    return cnt



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

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

if bfs()==False:

    print((-1))

else:

    s = (H * W - notcount(H, W, maze)) - (bfs() + 1)

    #bfs()

    print(s)

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

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

from collections import deque

def bfs(maze, sy, sx, gy, gx):

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

    que = deque([[sy, sx]])

    #print(que)

    d[sy][sx] = 0

    while que:

        y, x = que.popleft()

        if y == gy and x == gx:

            break

        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):

            ny, nx = y + j, x + k

            if 0 <= nx < w  and 0 <= ny < h  and d[ny][nx] == -1 and maze[ny][nx] != "#":

                d[ny][nx] = d[y][x] + 1

                que.append([ny, nx])

                

    return d

gx, gy = w - 1, h - 1

def counting(maze, h, w, aim):

    cnt = 0

    for i in range(h):

        for j in range(w):

            if maze[i][j] == aim:

                cnt += 1

    return cnt

a = bfs(maze, 0, 0, gy, gx)

if a[gy][gx] == -1:

    print((-1))

else:

    print((h * w - (counting(maze, h, w, '#') + (a[gy][gx] + 1))))

from collections import deque


def bfs():
    flag = False
    d = [[float("inf")] * W for i in range(H)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    que = deque([])
    que.append((0, 0))
    d[0][0] = 0
    while que:
        p = que.popleft()
        if p[0] == H - 1 and p[1] == W - 1:
            flag = True
            break
        for i in range(4):
            nx = p[1] + dx[i]
            ny = p[0] + dy[i]
            if (
                0 <= nx < W
                and 0 <= ny < H
                and maze[ny][nx] != "#"
                and d[ny][nx] == float("inf")
            ):
                que.append((ny, nx))
                d[ny][nx] = d[p[0]][p[1]] + 1
    if flag == False:
        return False
    return d[H - 1][W - 1]


def notcount(H, W, maze):
    cnt = 0
    for i in range(H):
        for j in range(W):
            if maze[i][j] == "#":
                cnt += 1
    return cnt


H, W = list(map(int, input().split()))
maze = [list(eval(input())) for i in range(H)]
if bfs() == False:
    print((-1))
else:
    s = (H * W - notcount(H, W, maze)) - (bfs() + 1)
    # bfs()
    print(s)

h, w = list(map(int, input().split()))
maze = [list(eval(input())) for _ in range(h)]
from collections import deque


def bfs(maze, sy, sx, gy, gx):
    d = [[-1] * w for _ in range(h)]
    que = deque([[sy, sx]])
    # print(que)
    d[sy][sx] = 0
    while que:
        y, x = que.popleft()
        if y == gy and x == gx:
            break
        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            ny, nx = y + j, x + k
            if 0 <= nx < w and 0 <= ny < h and d[ny][nx] == -1 and maze[ny][nx] != "#":
                d[ny][nx] = d[y][x] + 1
                que.append([ny, nx])
    return d


gx, gy = w - 1, h - 1


def counting(maze, h, w, aim):
    cnt = 0
    for i in range(h):
        for j in range(w):
            if maze[i][j] == aim:
                cnt += 1
    return cnt


a = bfs(maze, 0, 0, gy, gx)
if a[gy][gx] == -1:
    print((-1))
else:
    print((h * w - (counting(maze, h, w, "#") + (a[gy][gx] + 1))))

+h, w = list(map(int, input().split())) +maze = [list(eval(input())) for _ in range(h)] -def bfs(): -    flag = False -    d = [[float("inf")] * W for i in range(H)] -    dx = [1, 0, -1, 0] -    dy = [0, 1, 0, -1] -    que = deque([]) -    que.append((0, 0)) -    d[0][0] = 0 +def bfs(maze, sy, sx, gy, gx): +    d = [[-1] * w for _ in range(h)] +    que = deque([[sy, sx]]) +    # print(que) +    d[sy][sx] = 0 -        p = que.popleft() -        if p[0] == H - 1 and p[1] == W - 1: -            flag = True +        y, x = que.popleft() +        if y == gy and x == gx: -        for i in range(4): -            nx = p[1] + dx[i] -            ny = p[0] + dy[i] -            if ( -                0 <= nx < W -                and 0 <= ny < H -                and maze[ny][nx] != "#" -                and d[ny][nx] == float("inf") -            ): -                que.append((ny, nx)) -                d[ny][nx] = d[p[0]][p[1]] + 1 -    if flag == False: -        return False -    return d[H - 1][W - 1] +        for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]): +            ny, nx = y + j, x + k +            if 0 <= nx < w and 0 <= ny < h and d[ny][nx] == -1 and maze[ny][nx] != "#": +                d[ny][nx] = d[y][x] + 1 +                que.append([ny, nx]) +    return d -def notcount(H, W, maze): +gx, gy = w - 1, h - 1 + + +def counting(maze, h, w, aim): -    for i in range(H): -        for j in range(W): -            if maze[i][j] == "#": +    for i in range(h): +        for j in range(w): +            if maze[i][j] == aim: -H, W = list(map(int, input().split())) -maze = [list(eval(input())) for i in range(H)] -if bfs() == False: +a = bfs(maze, 0, 0, gy, gx) +if a[gy][gx] == -1: -    s = (H * W - notcount(H, W, maze)) - (bfs() + 1) -    # bfs() -    print(s) +    print((h * w - (counting(maze, h, w, "#") + (a[gy][gx] + 1))))