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 solve(ss: Vec<Vec<char>>) -> usize {
    let h = ss.len();
    let w = ss[0].len();

    let mut m = 0;
    for y in 0..h {
        for x in 0..w {
            if ss[y][x] != '#' {
                let c = _solve((y, x), &ss);
                if m < c {
                    m = c;
                }
            }
        }
    }

    m
}

fn _solve(start: (usize, usize), ss: &Vec<Vec<char>>) -> usize {
    let h = ss.len();
    let w = ss[0].len();

    let mut costs = vec![vec![std::usize::MAX; w]; h];

    let mut pos = vec![(0, start)];

    loop {
        if pos.is_empty() {
            break;
        }

        if let Some((c, (y, x))) = pos.pop() {
            if c < costs[y][x] {
                costs[y][x] = c;
                if y > 0 {
                    if ss[y - 1][x] != '#' {
                        pos.push((c + 1, (y - 1, x)));
                    }
                }

                if y + 1 < h {
                    if ss[y + 1][x] != '#' {
                        pos.push((c + 1, (y + 1, x)));
                    }
                }

                if x > 0 {
                    if ss[y][x - 1] != '#' {
                        pos.push((c + 1, (y, x - 1)));
                    }
                }

                if x + 1 < w {
                    if ss[y][x + 1] != '#' {
                        pos.push((c + 1, (y, x + 1)));
                    }
                }
            }
        }
    }

    let mut m = 0;
    for i in 0..h {
        for j in 0..w {
            if costs[i][j] < std::usize::MAX && m < costs[i][j] {
                m = costs[i][j];
            }
        }
    }

    m
}

#[test]
fn solve_test() {
    assert_eq!(
        solve(vec![
            "...".chars().collect::<Vec<_>>(),
            "...".chars().collect::<Vec<_>>(),
            "...".chars().collect::<Vec<_>>(),
        ]),
        4
    );

    assert_eq!(
        solve(vec![
            "...#.".chars().collect::<Vec<_>>(),
            ".#.#.".chars().collect::<Vec<_>>(),
            ".#...".chars().collect::<Vec<_>>(),
        ]),
        10
    );
}

#[allow(unused_macros)]
macro_rules! parse_line {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            iter.next().unwrap().parse::<$t>().unwrap()
        }
    );

    ( $( $t:ty), +) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ( $(iter.next().unwrap().parse::<$t>().unwrap()),* )
        }
    );
}

#[allow(unused_macros)]
macro_rules! read_line {
    () => {{
        let mut line = String::new();
        ::std::io::stdin().read_line(&mut line).unwrap();
        line.pop();
        line
    }};
}

#[allow(unused_macros)]
macro_rules! parse_vec {
    ( $t:ty ) => {{
        let mut line = String::new();
        ::std::io::stdin().read_line(&mut line).unwrap();
        let iter = line.split_whitespace();
        iter.map(|v| v.parse::<$t>().unwrap()).collect::<Vec<_>>()
    }};
}

fn main() {
    let (h, _w) = parse_line!(usize, usize);

    let ss = (0..h)
        .map(|_| read_line!().chars().collect::<Vec<char>>())
        .collect::<Vec<_>>();
    println!("{}", solve(ss));
}