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)

use std::io::Write;

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn main() {
    let (h, w): (usize, usize) = {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let s = s.trim_right().to_owned();
        let mut ws = s.split_whitespace();
        let a = ws.next().unwrap().parse().unwrap();
        let b = ws.next().unwrap().parse().unwrap();
        (a, b)
    };

    let maze: Vec<Vec<char>> = {
        let mut v = Vec::new();
        for _ in 0..h {
            let v_i = {
                let mut s = String::new();
                std::io::stdin().read_line(&mut s).unwrap();
                let s = s.trim_right().to_owned();
                s.chars().collect()
            };
            v.push(v_i)
        }
        v
    };

    const ROAD: char = '.';
    const WALL: char = '#';

    let mut index = 0;
    let mut coord_to_index = vec![vec![0; w]; h];
    for i in 0..h {
        for j in 0..w {
            if maze[i][j] == ROAD {
                coord_to_index[i][j] = index;
                index += 1;
            }
        }
    }
    // for i in 0..h {
    //     debugln!("{:?}", coord_to_index[i]);
    // }

    let num_node = index;
    // debugln!("num_node: {}", num_node);

    let mut graph: Vec<Vec<usize>> = vec![vec![0; num_node]; num_node];
    for i in 0..h {
        for j in 0..w {
            if maze[i][j] != ROAD {
                continue;
            }

            if i + 1 < h && maze[i + 1][j] == ROAD {
                let from = coord_to_index[i][j];
                let to = coord_to_index[i + 1][j];
                graph[from][to] = 1;
            }
            if j + 1 < w && maze[i][j + 1] == ROAD {
                let from = coord_to_index[i][j];
                let to = coord_to_index[i][j + 1];
                graph[from][to] = 1;
            }
            if i >= 1 && maze[i - 1][j] == ROAD {
                let from = coord_to_index[i][j];
                let to = coord_to_index[i - 1][j];
                graph[from][to] = 1;
            }
            if j >= 1 && maze[i][j - 1] == ROAD {
                let from = coord_to_index[i][j];
                let to = coord_to_index[i][j - 1];
                graph[from][to] = 1;
            }

            // debugln!("");
            // for i in 0..num_node {
            //     debugln!("{:?}", graph[i]);
            // }
        }
    }

    // for i in 0..num_node {
    //     debugln!("{:?}", graph[i]);
    // }

    let ans = graph_diameter(&graph);
    println!("{}", ans);
}

// Warshall-Floyd法
#[allow(unused)]
fn warshall_floyd(graph_mat: &[Vec<usize>]) -> Vec<Vec<usize>> {
    let n = graph_mat.len();

    let mut d = vec![vec![std::usize::MAX >> 2; n]; n];
    for i in 0..n {
        for j in 0..n {
            if graph_mat[i][j] == 1 {
                d[i][j] = 1;
            }
        }
    }
    for i in 0..n {
        d[i][i] = 0;
    }

    for k in 0..n {
        for i in 0..n {
            for j in 0..n {
                if d[i][j] > d[i][k] + d[k][j] {
                    d[i][j] = d[i][k] + d[k][j];
                }
            }
        }
    }

    d
}

// グラフの直径
#[allow(unused)]
fn graph_diameter(graph_mat: &[Vec<usize>]) -> usize {
    let d_mat = warshall_floyd(&graph_mat);
    let mut diameter = 0;
    for v in d_mat {
        for d in v {
            if d > diameter {
                diameter = d;
            }
        }
    }

    diameter
}