solve :: Int -> Int -> [(Int,Int)] -> (Int,Int) -> Int -> [String] -> Int
solve x y doneList end@(xx,yy) n list
  | x == xx && y == yy = n
  | x == 0 || y == 0 || (x-1) == (length $ head list) || (y-1) == length list || elem (x,y) doneList = 10000
      | otherwise = if '#' == (list !! (y-1)) !! (x-1) then 10000 else minimum [solve x (y+1) ((x,y):doneList) end (n+1) list, solve (x+1) y ((x,y):doneList) end (n+1) list]

main = do
  [x,y] <- fmap (map (read :: String -> Int) . words) getLine
  list <- fmap (lines) getContents
  len <- return $ sum $ map (length . filter ((==) '.')) list
  print $ (\x -> if x < 0 then -1 else x) $ (-) len $ solve 1 1 [] (y,x) 1 list