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: Wrong Answer)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use competitive::prelude::*;
use std::collections::VecDeque;

fn bfs_far(s: &Vec<Vec<char>>, sx: usize, sy: usize) -> (usize, usize) {
    let (h, w) = (s.len(), s[0].len());
    let mut costs = vec![vec![1000u32; w]; h];
    let mut q = VecDeque::<(usize, usize, u32)>::new();
    costs[sy][sx] = 0;
    q.push_back((sx, sy, 0));
    while let Some((x, y, c)) = q.pop_front() {
        let (x, y) = (x as isize, y as isize);
        let nx = [x-1, x, x+1, x];
        let ny = [y, y-1, y, y+1];
        for (&tx, &ty) in nx.iter().zip(ny.iter()) {
            if tx < 0 || ty < 0 {
                continue;
            }
            let (tx, ty) = (tx as usize, ty as usize);
            if w <= tx || h <= ty {
                continue;
            }
            if s[ty][tx] == '#' {
                continue;
            }
            if c+1 < costs[ty][tx] {
                q.push_back((tx, ty, c+1));
                costs[ty][tx] = c+1;
            }
        }
    }

    let (mut best_x, mut best_y) = (sx, sy);
    let mut best_cost = 0;
    for y in 0..h {
        for x in 0..w {
            if s[y][x] == '#' {
                continue;
            }
            if best_cost < costs[y][x] {
                best_x = x;
                best_y = y;
                best_cost = costs[y][x];
            }
        }
    }

    (best_x, best_y)
}

fn bfs(s: &Vec<Vec<char>>, sx: usize, sy: usize, gx: usize, gy: usize) -> u32 {
    let (h, w) = (s.len(), s[0].len());
    let mut costs = vec![vec![1000u32; w]; h];
    let mut q = VecDeque::<(usize, usize, u32)>::new();
    costs[sy][sx] = 0;
    q.push_back((sx, sy, 0));
    while let Some((x, y, c)) = q.pop_front() {
        if (x, y) == (gx, gy) {
            return costs[y][x];
        }
        let (x, y) = (x as isize, y as isize);
        let nx = [x-1, x, x+1, x];
        let ny = [y, y-1, y, y+1];
        for (&tx, &ty) in nx.iter().zip(ny.iter()) {
            if tx < 0 || ty < 0 {
                continue;
            }
            let (tx, ty) = (tx as usize, ty as usize);
            if w <= tx || h <= ty {
                continue;
            }
            if s[ty][tx] == '#' {
                continue;
            }
            if c+1 < costs[ty][tx] {
                q.push_back((tx, ty, c+1));
                costs[ty][tx] = c+1;
            }
        }
    }
    unreachable!()
}

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

    for y in 0..h {
        for x in 0..w {
            if s[y][x] == '#' {
                continue;
            }
            let (sx, sy) = bfs_far(&s, x, y);
            let (gx, gy) = bfs_far(&s, sx, sy);
            let ans = bfs(&s, sx, sy, gx, gy);
            println!("{}", ans);
            return;
        }
    }
}
*/

fn main() {
    let exe = "/tmp/bin964A58CD";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";