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;
use std::str::FromStr;
use std::collections::VecDeque;

type Point = (usize, usize);

fn input_tuple() -> (usize, usize) {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    let vec: Vec<usize> = line.split_whitespace().map(|s| s.parse().unwrap()).collect();
    (vec[0], vec[1])
}

fn input<T:FromStr>() -> T {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    line.trim().parse().ok().unwrap()
}

fn push_queue(x: usize, y: usize, queue: &mut VecDeque<Point>, count: &mut Vec<Vec<usize>>, field: &mut Vec<Vec<char>>) {
    if field[y][x-1] == '.' {        
        queue.push_back((y, x-1));
        count[y][x-1] = count[y][x] + 1;
        field[y][x-1] = '#';
    }
    if field[y][x+1] == '.' {
        queue.push_back((y, x+1));
        count[y][x+1] = count[y][x] + 1;
        field[y][x+1] = '#';
    }
    if field[y-1][x] == '.' {
        queue.push_back((y - 1, x));
        count[y-1][x] = count[y][x] + 1;
        field[y-1][x] = '#';
    }
    if field[y+1][x] == '.' {
        queue.push_back((y + 1, x));
        count[y+1][x] = count[y][x] + 1;
        field[y+1][x] = '#';
    }
}

fn bfs(goal: Point, grid: &Vec<Vec<char>>, count: &mut Vec<Vec<usize>>) {
    let mut queue: VecDeque<Point> = VecDeque::new();
    let mut grid_buf: Vec<Vec<char>> = grid.to_vec();
    queue.push_back(goal);
    while (queue.len() > 0) {
        let point = queue.pop_front().unwrap();
        grid_buf[point.0][point.1] = '#';
        push_queue(point.1, point.0, &mut queue, count, &mut grid_buf);
    }
}

fn main() {
    let (h, w) = input_tuple();
    let mut grid: Vec<Vec<char>> = Vec::new();
    grid.push(vec!['#'; (w+2)]);
    for _ in 0..h {
        let s: String = input();
        let mut s: Vec<char> = s.chars().collect();
        s.insert(0, '#');
        s.push('#');
        grid.push(s);
    }
    grid.push(vec!['#'; (w+2)]);
    // println!("{:?}", grid);

    let mut max = 0;
    for i_g in 1..(h+1) {
        for j_g in 1..(w+1) {
            // println!("{}, {}", i_g, j_g);
            if grid[i_g][j_g] == '#' {
                continue;
            }
            let mut count: Vec<Vec<usize>> = vec![vec![0; grid[0].len()]; grid.len()];
            bfs((i_g, j_g), &grid, &mut count);        
            // println!("{:?}", count);
            for i in count {
                for j in i {
                    if j > max {
                        max = j;
                    }
                }
            }
        }
    }
    println!("{}", max);    
}