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



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

fn get_vec<T : FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    return s.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect();
}

fn bfs(maze : &Vec<Vec<bool>>, h : usize, w : usize, stt : (usize,usize), tar : (usize,usize)) -> i32 {
    let mut dst = Vec::new();
    for i in 0..h {
        let v = vec!(false; w);
        dst.push(v);
    }
    dst[stt.0][stt.1] = true;
    let mut que : VecDeque<((usize,usize),i32)> = VecDeque::new();
    que.push_back((stt,0));
    let mut d = 0;
    while !que.is_empty() {

        let x = que.pop_front();
        let x0 = (x.unwrap().0).0;
        let x1 = (x.unwrap().0).1;
        let ds = x.unwrap().1;
        //if (stt == (1,1) && tar == (3,3)){
        //    println!("{} {} {} ", d, x0, x1);
        //}
        dst[x0][x1] = true;
        if x.unwrap().0 == tar {
            return ds;
        }
        if maze[x0-1][x1] && !dst[x0-1][x1] {
            que.push_back(((x0-1, x1), ds+1));
            dst[x0-1][x1] = true;
        }
        if maze[x0+1][x1] && !dst[x0+1][x1] {
            que.push_back(((x0+1, x1), ds+1));
            dst[x0+1][x1] = true;
        }
        if maze[(x0)][(x1)-1] && !dst[x0][x1-1] {
            que.push_back(((x0, x1-1), ds+1));
            dst[x0][x1-1] = true;
        }
        if maze[(x0)][(x1)+1] && !dst[x0][x1+1] {
            que.push_back(((x0, x1+1), ds+1));
            dst[x0][x1+1] = true;
        }

    }
    return d;

}

fn main() {
    let hw : Vec<usize> = get_vec();
    let mut maze : Vec<Vec<bool>> = Vec::new();
    let dum1 = vec!(false; (hw[1]+2));
    maze.push(dum1);
    for i in 0..hw[0] {
        let v : String = get_one();
        let mut row : Vec<bool> = Vec::new();
        row.push(false);
        for c in v.chars() {
            if c == '.' {
                row.push(true);
            }
            else {
                row.push(false);
            }
        }
        row.push(false);
        maze.push(row);
    }
    let dum2= vec!(false; (hw[1]+2));
    maze.push(dum2);
    let mut ans = 0;
    for i1 in 0..hw[0]+2 {
        for i2 in 0..hw[1]+2 {
            for j1 in 0..hw[0]+2 {
                for j2 in 0..hw[1]+2 {
                    if maze[i1][i2] && maze[j1][j2] {
                        let a = bfs(&maze, hw[0]+2, hw[1]+2, (i1,i2), (j1,j2));
                        ans = std::cmp::max(ans, a);
                    }
                }
            }
        }
    }

    println!("{}", ans);
}