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

s = []

for i in range(H):

    S = list(input())
    s.append(S)

Q = [[0,0]]
shnum = 0

#print (s)

for i in range(H):

    for j in range(W):

        if s[i][j] == "#":

            shnum += 1


while len(Q) > 0:

    #print (Q)
    #print (s)

    now = Q.pop(0)

    if now[0] == 0 and now[1] == 0:
        s[0][0] = 1
        Q.append([0,1])
        Q.append([1,0])

    elif s[now[0]][now[1]] == "#":
        continue;

    else:

        if s[now[0]][now[1]] == ".":

            far = 10000

        else:

            far = s[now[0]][now[1]]

        flag = 0

        if now[0]-1 >= 0 and s[now[0]-1][now[1]] not in [-1,"#","."]:

            if far > s[now[0]-1][now[1]]+1:
                far = s[now[0]-1][now[1]]+1
                flag = 1

        if now[1]-1 >= 0 and s[now[0]][now[1]-1] not in [-1,"#","."]:

            if far > s[now[0]][now[1]-1]+1:
                far = s[now[0]][now[1]-1]+1
                flag = 2

        if now[0]+1 < H and s[now[0]+1][now[1]] not in [-1,"#","."]:

            if far > s[now[0]+1][now[1]]+1:
                far = s[now[0]+1][now[1]]+1
                flag = 3

        if now[1]+1 < W and s[now[0]][now[1]+1] not in [-1,"#","."]:

            if far > s[now[0]][now[1]+1]+1:
                far = s[now[0]][now[1]+1]+1
                flag = 4

        if flag != 0:
            
            s[now[0]][now[1]] = far

            if now[0]-1 >= 0 and flag != 1:

                Q.append([now[0]-1,now[1]])

            if now[1]-1 >= 0 and flag != 2:

                Q.append([now[0],now[1]-1])

            if now[0]+1 < H and flag != 3:

                Q.append([now[0]+1,now[1]])

            if now[1]+1 < W and flag != 4:

                Q.append([now[0],now[1]+1])


if s[-1][-1] == -1:
    print (-1)

else:
    print (H*W - s[-1][-1] - shnum)



### FIXED CODE ###


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

s = []

for i in range(H):

    S = list(input())
    s.append(S)

Q = [[0,0]]
shnum = 0

#print (s)

for i in range(H):

    for j in range(W):

        if s[i][j] == "#":

            shnum += 1


while len(Q) > 0:

    #print (Q)
    #print (s)

    now = Q.pop(0)

    if now[0] == 0 and now[1] == 0:
        s[0][0] = 1
        Q.append([0,1])
        Q.append([1,0])

    elif s[now[0]][now[1]] == "#":
        continue;

    else:

        if s[now[0]][now[1]] == ".":

            far = 10000

        else:

            far = s[now[0]][now[1]]

        flag = 0

        if now[0]-1 >= 0 and s[now[0]-1][now[1]] not in [-1,"#","."]:

            if far > s[now[0]-1][now[1]]+1:
                far = s[now[0]-1][now[1]]+1
                flag = 1

        if now[1]-1 >= 0 and s[now[0]][now[1]-1] not in [-1,"#","."]:

            if far > s[now[0]][now[1]-1]+1:
                far = s[now[0]][now[1]-1]+1
                flag = 2

        if now[0]+1 < H and s[now[0]+1][now[1]] not in [-1,"#","."]:

            if far > s[now[0]+1][now[1]]+1:
                far = s[now[0]+1][now[1]]+1
                flag = 3

        if now[1]+1 < W and s[now[0]][now[1]+1] not in [-1,"#","."]:

            if far > s[now[0]][now[1]+1]+1:
                far = s[now[0]][now[1]+1]+1
                flag = 4

        if flag != 0:
            
            s[now[0]][now[1]] = far

            if now[0]-1 >= 0 and flag != 1:

                Q.append([now[0]-1,now[1]])

            if now[1]-1 >= 0 and flag != 2:

                Q.append([now[0],now[1]-1])

            if now[0]+1 < H and flag != 3:

                Q.append([now[0]+1,now[1]])

            if now[1]+1 < W and flag != 4:

                Q.append([now[0],now[1]+1])


if s[-1][-1] == -1:
    print (-1)

else:

    if s[-1][-1] == ".":
        print (-1)
    else:
        print (H*W - s[-1][-1] - shnum)