-- capture
local h, w = io.read("*n", "*n", "*l")
local s = h * w
local str = ""
local t = {}
local white = 0
for i = 1, h do
  str = io.read()
  for j = 1, w do
    if(string.sub(str, j, j) == ".") then
      t[(i - 1) * w + j] = 0
      white = white + 1
    else
      t[(i - 1) * w + j] = -1
    end
  end
end
t[1] = 1
local check_pos = {1}
local rem_count, task_idx, step = 1, 1, 1
while(0 < rem_count) do
  local idx = check_pos[task_idx]
  rem_count, task_idx = rem_count - 1, task_idx + 1
  -- left
  if(idx % w ~= 1) then
    if(t[idx - 1] == 0) then t[idx - 1] = t[idx] + 1; rem_count = rem_count + 1; table.insert(check_pos, idx - 1) end
  end
  -- right
  if(idx % w ~= 0) then
    if(t[idx + 1] == 0) then t[idx + 1] = t[idx] + 1; rem_count = rem_count + 1; table.insert(check_pos, idx + 1) end
  end
  -- up
  if(w < idx) then
    if(t[idx - w] == 0) then t[idx - w] = t[idx] + 1; rem_count = rem_count + 1; table.insert(check_pos, idx - w) end
  end
  -- down
  if(idx <= s - w) then
    if(t[idx + w] == 0) then t[idx + w] = t[idx] + 1; rem_count = rem_count + 1; table.insert(check_pos, idx + w) end
  end
end
if(0 < t[s]) then print(white - t[s]) else print(-1) end

