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)

fn read<T: std::str::FromStr>(
    stdin_lock: &mut std::io::StdinLock,
    buf: &mut Vec<u8>,
    delimiter: u8,
) -> T {
    buf.clear();
    let l = std::io::BufRead::read_until(stdin_lock, delimiter, buf).unwrap();
    buf.truncate(l - 1); // remove delimiter
    let s = unsafe { std::str::from_utf8_unchecked(&buf) };
    s.parse().unwrap_or_else(|_| panic!("read"))
}

fn main() {
    use std::cmp::min;

    let stdin = std::io::stdin();
    let mut stdin_lock = stdin.lock();
    let mut buf: Vec<u8> = Vec::new();
    let h: usize = read(&mut stdin_lock, &mut buf, b' ');
    let w: usize = read(&mut stdin_lock, &mut buf, b'\n');
    let mut rv = vec![false; h * w];
    for y in 0..h {
        let s: String = read(&mut stdin_lock, &mut buf, b'\n');
        let lv: Vec<char> = s.chars().collect();
        for x in 0..w {
            rv[y * w + x] = lv[x] == '.';
        }
    }

    let mut start = (0, 0);
    for y in 0..h {
        let mut found = false;
        for x in 0..w {
            let i = y * w + x;
            if rv[i] {
                start = (x, y);
                found = true;
                break;
            }
        }
        if found {
            break;
        }
    }

    let mut cv: Vec<Option<usize>> = vec![None; h * w];
    let mut queue = Vec::new();
    cv[start.1 * w + start.0] = Some(0);
    queue.push((start, 0));
    while let Some(((x, y), c)) = queue.pop() {
        let cn = c + 1;

        // top
        if y > 0 {
            let xn = x;
            let yn = y - 1;
            let i = yn * w + xn;
            if rv[i] {
                if let Some(co) = cv[i] {
                    if cn < co {
                        cv[i] = Some(min(co, cn));
                        queue.push(((xn, yn), min(co, cn)));
                    }
                } else {
                    cv[i] = Some(cn);
                    queue.push(((xn, yn), cn));
                }
            }
        }

        // right
        if x + 1 < w {
            let xn = x + 1;
            let yn = y;
            let i = yn * w + xn;
            if rv[i] {
                if let Some(co) = cv[i] {
                    if cn < co {
                        cv[i] = Some(min(co, cn));
                        queue.push(((xn, yn), min(co, cn)));
                    }
                } else {
                    cv[i] = Some(cn);
                    queue.push(((xn, yn), cn));
                }
            }
        }

        // bottom
        if y + 1 < h {
            let xn = x;
            let yn = y + 1;
            let i = yn * w + xn;
            if rv[i] {
                if let Some(co) = cv[i] {
                    if cn < co {
                        cv[i] = Some(min(co, cn));
                        queue.push(((xn, yn), min(co, cn)));
                    }
                } else {
                    cv[i] = Some(cn);
                    queue.push(((xn, yn), cn));
                }
            }
        }

        // left
        if x > 0 {
            let xn = x - 1;
            let yn = y;
            let i = yn * w + xn;
            if rv[i] {
                if let Some(co) = cv[i] {
                    if cn < co {
                        cv[i] = Some(min(co, cn));
                        queue.push(((xn, yn), min(co, cn)));
                    }
                } else {
                    cv[i] = Some(cn);
                    queue.push(((xn, yn), cn));
                }
            }
        }
    }

    let mut mp = (0, (0, 0));
    for y in 0..h {
        for x in 0..w {
            let i = y * w + x;
            if let Some(v) = cv[i] {
                if v > mp.0 {
                    mp = (v, (x, y));
                }
            }
        }
    }

    cv = vec![None; h * w];
    start = mp.1;
    cv[start.1 * w + start.0] = Some(0);
    queue.push((mp.1, 0));
    while let Some(((x, y), c)) = queue.pop() {
        let cn = c + 1;

        // top
        if y > 0 {
            let xn = x;
            let yn = y - 1;
            let i = yn * w + xn;
            if rv[i] {
                if let Some(co) = cv[i] {
                    if cn < co {
                        cv[i] = Some(min(co, cn));
                        queue.push(((xn, yn), min(co, cn)));
                    }
                } else {
                    cv[i] = Some(cn);
                    queue.push(((xn, yn), cn));
                }
            }
        }

        // right
        if x + 1 < w {
            let xn = x + 1;
            let yn = y;
            let i = yn * w + xn;
            if rv[i] {
                if let Some(co) = cv[i] {
                    if cn < co {
                        cv[i] = Some(min(co, cn));
                        queue.push(((xn, yn), min(co, cn)));
                    }
                } else {
                    cv[i] = Some(cn);
                    queue.push(((xn, yn), cn));
                }
            }
        }

        // bottom
        if y + 1 < h {
            let xn = x;
            let yn = y + 1;
            let i = yn * w + xn;
            if rv[i] {
                if let Some(co) = cv[i] {
                    if cn < co {
                        cv[i] = Some(min(co, cn));
                        queue.push(((xn, yn), min(co, cn)));
                    }
                } else {
                    cv[i] = Some(cn);
                    queue.push(((xn, yn), cn));
                }
            }
        }

        // left
        if x > 0 {
            let xn = x - 1;
            let yn = y;
            let i = yn * w + xn;
            if rv[i] {
                if let Some(co) = cv[i] {
                    if cn < co {
                        cv[i] = Some(min(co, cn));
                        queue.push(((xn, yn), min(co, cn)));
                    }
                } else {
                    cv[i] = Some(cn);
                    queue.push(((xn, yn), cn));
                }
            }
        }
    }

    let mut mp = (0, (0, 0));
    for y in 0..h {
        for x in 0..w {
            let i = y * w + x;
            if let Some(v) = cv[i] {
                if v > mp.0 {
                    mp = (v, (x, y));
                }
            }
        }
    }

    println!("{}", mp.0);
}