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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::collections::VecDeque;
fn read<T: std::str::FromStr>() -> T {
  let mut s = String::new();
  std::io::stdin().read_line(&mut s).ok();
  s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
  read::<String>().split_whitespace()
    .map(|e| e.parse().ok().unwrap()).collect()
}

fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
  (0..n).map(|_| read_vec()).collect()
}

fn main() {
  let (h, w) = {
    let tmp: Vec<usize> = read_vec();
    (tmp[0], tmp[1])
  };
  let mut s: Vec<Vec<char>> = Vec::new();
  for _ in 0..h {
    let tmp: Vec<char> = read::<String>().chars().collect();
    s.push(tmp);
  }

  let mut deque: VecDeque<(usize, usize, usize)> = VecDeque::new();
  let mut max = 0;
  let mut tmp_max = 0;

  for i in 0..h {
    for j in 0..w {
      let mut dist: Vec<Vec<bool>> = vec![vec![false; w]; h];
      if s[i][j] == '#' { continue; }
      deque.push_back((i, j, 0));
      loop {
        match deque.pop_front() {
          None => { break; }
          Some((x, y, d)) => {
            dist[x][y] = true;
            if x>0 && !dist[x-1][y] && s[x-1][y] != '#' {
              deque.push_back((x-1, y, d+1));
              tmp_max = d+1;
            }
            if x<h-1 && !dist[x+1][y] && s[x+1][y] != '#' {
              deque.push_back((x+1, y, d+1));
              tmp_max = d+1;
            }
            if y>0 && !dist[x][y-1] && s[x][y-1] != '#' {
              deque.push_back((x, y-1, d+1));
              tmp_max = d+1;
            }
            if y<w-1 && !dist[x][y+1] && s[x][y+1] != '#'{
              deque.push_back((x, y+1, d+1));
              tmp_max = d+1;
            }
          }
        }
      }
      if tmp_max > max { max = tmp_max; }
      tmp_max = 0;
    }
  }
  println!("{}", max);
}