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)

fn serial_to_point(serial: usize, w: usize) -> (usize, usize) {
    (serial%w, serial/w)
}

fn calc_distance(s: usize,  g: usize, m: &Vec<Vec<char>>) -> usize {
    let h = m.len() as isize;
    let w = m[0].len() as isize;
    let dist_none = w*h*10;
    let mut dist_map = vec![vec![dist_none; w as usize]; h as usize];

    let (sx_t, sy_t) = serial_to_point(s, w as usize);
    let (sx, sy) = (sx_t as isize, sy_t as isize);
    let (gx_t, gy_t) = serial_to_point(g, w as usize);
    let (gx, gy) = (gx_t as isize, gy_t as isize);

    let mut q = std::collections::VecDeque::<(isize, isize)>::new();
    dist_map[sy as usize][sx as usize] = 0;
    q.push_front((sx, sy));

    loop {
        if let Some((x, y)) = q.pop_back() {
            let dist = dist_map[y as usize][x as usize];
            if (x, y) == (gx, gy) {
                return dist as usize;
            }

            for i in 0..4 {
                let (nx, ny) = match i {
                    0 => (x-1, y),
                    1 => (x, y-1),
                    2 => (x+1, y),
                    _ => (x, y+1),
                };

                if nx < 0 || nx >= w || ny < 0 || ny >= h {
                    continue;
                }

                if dist_map[ny as usize][nx as usize] != dist_none {
                    continue;
                }

                if m[ny as usize][nx as usize] != '.' {
                    continue;
                }

                dist_map[ny as usize][nx as usize] = dist+1;
                q.push_front((nx, ny));
            }
        }
        else {
            break
        }
    }

    // unreachable to goal
    0
}

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();
    let h: usize = iter.next().unwrap().parse().unwrap();
    let w: usize = iter.next().unwrap().parse().unwrap();

    let mut maze = vec![vec!['.'; w]; h];

    for y in 0..h {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        maze[y] = buf.trim().chars().collect();
    }

    let mut ans = 0usize;
    for s in 0..(w*h-1) {
        let (sx, sy) = serial_to_point(s, w);
        if maze[sy][sx] == '.' {
            for g in (s+1)..(w*h) {
                let (gx, gy) = serial_to_point(g, w);
                if maze[gy][gx] == '.' {
                    ans = std::cmp::max(ans, calc_distance(s, g, &maze));
                }
            }
        }
    }

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