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: Time Limit Exceeded)

use std::io::stdin;
use std::cmp::{
    min,
    max,
};
use std::time::SystemTime;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::collections::HashSet;

#[allow(dead_code)]
fn read_line() -> String {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
fn read<T>() -> T
where T: std::str::FromStr
{
    read_line().parse::<T>().ok().unwrap()
}

#[allow(dead_code)]
fn read_vec<T>() -> Vec<T>
where T: std::str::FromStr
{
    read_line().split(" ").map(|n| n.parse::<T>().ok().unwrap()).collect()
}

#[allow(dead_code)]
fn read_chars() -> Vec<char>
{
    read_line().chars().collect()
}

#[allow(dead_code)]
fn print_time() -> () {
    println!("{:?}", SystemTime::now());
}

trait BinarySearchLeft<T> {
    fn binary_search_left(&self, x: &T) -> Result<usize, usize>;
}

impl<T> BinarySearchLeft<T> for Vec<T>
where T: std::cmp::Ord + std::fmt::Debug
{
    fn binary_search_left(&self, x: &T) -> Result<usize, usize> {
        let index = self.binary_search(x);
        match index {
            Ok(index) => {
                for i in (1..index+1).rev() {
                    if self[i] != self[i - 1] {
                        return Ok(i - 1)
                    }
                }
                
                Ok(index)
            },
            _ => index,
        }
    }
}

#[allow(unused_variables)]
fn main() {
    let nm: Vec<usize> = read_vec();
    let mut v = vec![];
    for _ in 0..nm[0] {
        v.push(read_chars());
    }

    let mut ans = 0;
    for _i in 0..nm[0] {
        for _j in 0..nm[1] {
            for _k in 0..nm[0] {
                for _l in 0..nm[1] {
                    if _i == _j && _k == _l {
                        continue;
                    }

                    if v[_i][_j] == '#' || v[_k][_l] == '#' {
                        continue;
                    }

                    let mut m = vec![vec![1000000; nm[1]]; nm[0]];
                    v[_i][_j] = 'S';
                    v[_k][_l] = 'G';

                    let mut s = S {
                        map: v.clone(),
                        nm: nm.clone(),
                        m: m,
                    };
                    s.f(_i, _j, 0);

                    let p = s.m[_k][_l];
                    if p != 1000000 {
                        ans = max(ans, p);
                    }

                    v[_i][_j] = '.';
                    v[_k][_l] = '.';
                }
            }
        }
    }
    println!("{}", ans);
}

struct S {
    map: Vec<Vec<char>>,
    nm: Vec<usize>,
    m: Vec<Vec<usize>>,
}

impl S {
    fn f(&mut self, i: usize, j: usize, c: usize) {
        if self.m[i][j] <= c {
            return;
        }
        if self.map[i][j] == 'G' {
            self.m[i][j] = c;
            return;
        }
        if self.map[i][j] == '#' {
            return;
        }
        self.m[i][j] = c;

        if i + 1 < self.nm[0] {
            self.f(i + 1, j, c + 1);
        }
        if i >= 1 {
            self.f(i - 1, j, c + 1);
        }
        if j + 1 < self.nm[1] {
            self.f(i, j + 1, c + 1);
        }
        if j >= 1 {
            self.f(i, j - 1, c + 1);
        }
    }
}