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)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
 
    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::collections::VecDeque;

fn main() {
    input! {
        h: usize,
        w: usize,
        maze: [chars; h],
    }

    let mut maximum = 0;

    for (i, mm) in maze.iter().enumerate() {
        for (j, m) in mm.iter().enumerate() {
            let mut dist = vec![vec![-1; w]; h];
            dist[i][j] = 0;
            if *m == '.' {
                let (_, length) = bfs((i, j), &maze, &mut dist);
                if length > maximum {
                    maximum = length;
                }
                //println!("length: {}", length);
            }
        }
    }

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

fn bfs(start: (usize, usize), maze: &Vec<Vec<char>>, dist: &mut Vec<Vec<isize>>) -> ((usize, usize), isize)
{
    //let prev_x = vec![vec![-1; width]; height];
    //let prev_y = vec![vec![-1; width]; height];

    let height = maze.len();
    let width = maze[0].len();
    let mut maximum = 0;
    let mut goal = (0, 0);

    let dx = vec![1, 0, -1, 0];
    let dy = vec![0, 1, 0, -1];

    let mut que = VecDeque::new();
    que.push_back(start);

    while !que.is_empty() {
        let current_pos = que.pop_front().unwrap();
        let x = current_pos.0;
        let y = current_pos.1;
    
        for d in 0..4 {
            let next_x = x as isize + dx[d];
            let next_y = y as isize + dy[d];
            if next_x < 0 || next_x >= height as isize || next_y < 0 || next_y >= width as isize {
                continue;
            }
            if maze[next_x as usize][next_y as usize] == '#' {
                continue;
            }
            if dist[next_x as usize][next_y as usize] == -1 {
                que.push_back((next_x as usize, next_y as usize));
                dist[next_x as usize][next_y as usize] = dist[x][y] + 1;
                if dist[next_x as usize][next_y as usize] > maximum {
                    maximum = dist[next_x as usize][next_y as usize];
                    goal = (next_x as usize, next_y as usize);
                    //println!("goal: ({}, {}) , length: {}", goal.0, goal.1, maximum);
                }
                //prev_x[next_x as usize][next_y as usize] = x as isize;
                //prev_y[next_x as usize][next_y as usize] = y as isize;
            }
        }
    }

    (goal, maximum)
}