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: Wrong Answer)

use std::io;
use std::cmp;
use std::collections::VecDeque;


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

    let mut ans = 0;
    if h == 1 && w == 1 { println!("0"); return; }
    for sy in 0..h { for sx in 0..w { for gy in 0..h { for gx in 0..w {
        if g[sy][sx] == '#' || g[gy][gx] == '#' { continue; }
        let mut q = VecDeque::new();
        let mut done = vec![vec![false; w]; h];
        let mut min = 1_000_000_000;
        done[sy][sx] = true;
        q.push_back((sy, sx, 1));
        while let Some((y, x, cost)) = q.pop_front() {
            let mut p = Vec::new();
            if y > 0 && g[y - 1][x] != '#' { p.push((y - 1, x)); }
            if y < h - 1 && g[y + 1][x] != '#' { p.push((y + 1, x)); }
            if x > 0 && g[y][x - 1] != '#' { p.push((y, x- 1)); }
            if x < w - 1 && g[y][x + 1] != '#' { p.push((y, x + 1)); }
            for (vy, vx) in p {
                if vy == gy && vx == gx { min = cmp::min(min, cost); continue; }
                if done[vy][vx] { continue; }
                done[vy][vx] = true;
                q.push_back((vy, vx, cost + 1));
            }
        }
        ans = cmp::max(ans, min);
    }}}}
    println!("{}", ans);
}


#[allow(dead_code)]
fn read<T>() -> Vec<T>
where T:
std::str::FromStr,
T::Err: std::fmt::Debug {

    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    buf.split_whitespace()
        .map(|s| s.trim().parse().unwrap())
        .collect()
}

#[allow(dead_code)]
fn read_one<T>() -> T
where T:
std::str::FromStr,
T::Err: std::fmt::Debug {

    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    buf.trim().parse().unwrap()
}