Problem:
Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.

Solution in Haskell (GHC 7.10.3) (judge verdict: Wrong Answer)

import Data.Array
import qualified Data.Map.Strict as Map

main = do
  h:w:_ <- map read . words <$> getLine :: IO [Int]
  ss <- lines <$> getContents :: IO [String]
  putStrLn $ show $ solveAll $ readAsMatrix h w ss

readAsMatrix :: Int -> Int -> [String] -> Array Int (Array Int Bool)
readAsMatrix n m ss = array (1, n) $ zip [1..n] $ map line ss
    where line s = array (1, m) $ zip [1..m] $ map figToInt s
          figToInt '#' = False
          figToInt '.' = True

solveAll :: Array Int (Array Int Bool) -> Int
solveAll maze = maximum $ let n = length $ indices maze
                              m = length $ indices (maze ! 1)
                          in [ solve n m (i, j) maze | i <- indices maze, j <- indices (maze ! i) ]

solve :: Int -> Int -> (Int, Int) -> Array Int (Array Int Bool) -> Int
solve n m (i,j) maze = Map.foldr max 0 $ bfs [((i, j), 0)] Map.empty
    where bfs :: [((Int, Int), Int)] -> Map.Map (Int, Int) Int -> Map.Map (Int, Int) Int
          bfs [] a = a
          bfs (((i,j),d):q) a = case  Map.lookup (i, j) a of
                                  Just _ -> bfs q a
                                  Nothing -> bfs (q ++ r) $ Map.insert (i, j) d a
                                      where r = [ ((y, x), d + 1) | (y,x) <- around (i, j), (maze ! y) ! x, Map.lookup (y, x) a == Nothing ]
                                            around (i, j) = [ (y, x) | (y,x) <- [(i+1, j), (i, j-1), (i-1, j), (i, j+1)], 0 < y, y <= n, 0 < x, x <= m ]