white = 0
HW = parse.(split(readline()))
H,W = HW[1], HW[2]
C = []
for i in 1:H
s = chomp(readline())
white += count(c -> c == '.', s)
push!(C,s)
end
st = [1,1]
go = [H ,W]
dx = [1,-1,0,0]
dy = [0,0,1,-1]
function BFS(graph,start,goal)
que = []
push!(que,start)
dist = [[-1 for i in 1:W] for j in 1:H]
dist[start[1]][start[2]] = 0
while isempty(que) == false
x,y = shift!(que)
if [x,y] == goal
break
else
for i in 1:4
if 0 <x+dx[i] <=H && 0 <y+dy[i] <=W
if C[x+dx[i]][y+dy[i]] != '#' && dist[x+dx[i]][y+dy[i]] == -1
push!(que,[x+dx[i],y+dy[i]])
dist[x+dx[i]][y+dy[i]] = dist[x][y]+1
end
end
end
end
end
dist[goal[1]][goal[2]]
end
if BFS(C,st,go) == -1
print(-1)
else
print(white - BFS(C,st,go) - 1)
end