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: Accepted)

fn main() {
    use std::io::Read;
    let mut input = String::new();
    std::io::stdin().read_to_string(&mut input).unwrap();
    println!("{}", solve(&input));
}

fn solve(input: &str) -> String {
    let mut iter = input.split_whitespace();
    let h: usize = iter.next().unwrap().parse().unwrap();
    let w: usize = iter.next().unwrap().parse().unwrap();
    let maze: Vec<Vec<char>> = (0..h)
        .map(|_| iter.next().unwrap().chars().collect())
        .collect();
    let mut solver = Solver {
        h: h,
        w: w,
        maze: maze,
        distance: vec![vec![0; w]; h],
    };
    let ans = solver.solve();
    format!("{}\n", ans)
}

struct Solver {
    h: usize,
    w: usize,
    maze: Vec<Vec<char>>,
    distance: Vec<Vec<i32>>,
}

impl Solver {
    fn solve(&mut self) -> i32 {
        let mut max = 0;
        for i in 0..self.h {
            for j in 0..self.w {
                if self.maze[i][j] == '.' {
                    self.reset();
                    self.dfs(i, j, 0);
                    max = std::cmp::max(
                        max,
                        *self
                            .distance
                            .iter()
                            .map(|row| row.iter().max().unwrap())
                            .max()
                            .unwrap(),
                    );
                }
            }
        }
        max
    }
    fn reset(&mut self) {
        for i in 0..self.h {
            for j in 0..self.w {
                if self.maze[i][j] == '.' {
                    self.distance[i][j] = std::i32::MAX;
                } else {
                    self.distance[i][j] = -1;
                }
            }
        }
    }
    fn dfs(&mut self, i: usize, j: usize, d: i32) {
        if self.distance[i][j] > d {
            self.distance[i][j] = d;
            if i > 0 {
                self.dfs(i - 1, j, d + 1);
            }
            if j > 0 {
                self.dfs(i, j - 1, d + 1);
            }
            if i + 1 < self.h {
                self.dfs(i + 1, j, d + 1);
            }
            if j + 1 < self.w {
                self.dfs(i, j + 1, d + 1);
            }
        }
    }
}