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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Collection;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Deque;
import java.util.ArrayDeque;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        MyScanner in = new MyScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        D solver = new D();
        solver.solve(1, in, out);
        out.close();
    }

    static class D {
        public void solve(int testNumber, MyScanner in, PrintWriter out) {
            int H = in.nextInt();
            int W = in.nextInt();
            char[][] S = new char[H][W];
            for (int i = 0; i < H; i++) {
                S[i] = in.next().toCharArray();
            }

            int sx = -1, sy = -1;
            for (int i = 0; i < H; i++) {
                for (int j = 0; j < W; j++) {
                    if (S[i][j] == '.') {
                        sy = i;
                        sx = j;
                        break;
                    }
                }
                if (sx >= 0) {
                    break;
                }
            }
            int[] m1 = calc(S, sx, sy);
            int[] m2 = calc(S, m1[0], m1[1]);
            out.println(m2[2] - 1);
        }

        private int[] calc(char[][] S, int sx, int sy) {
            int H = S.length;
            int W = S[0].length;
            int[][] cnt = new int[H][W];
            cnt[sy][sx] = 1;
            Deque<int[]> que = new ArrayDeque<>();
            que.add(new int[]{sx, sy});
            while (!que.isEmpty()) {
                int[] s = que.removeFirst();
                if (s[0] > 0 && cnt[s[1]][s[0] - 1] == 0 && S[s[1]][s[0] - 1] == '.') {
                    cnt[s[1]][s[0] - 1] = cnt[s[1]][s[0]] + 1;
                    que.add(new int[]{s[0] - 1, s[1]});
                }
                if (s[0] < W - 1 && cnt[s[1]][s[0] + 1] == 0 && S[s[1]][s[0] + 1] == '.') {
                    cnt[s[1]][s[0] + 1] = cnt[s[1]][s[0]] + 1;
                    que.add(new int[]{s[0] + 1, s[1]});
                }
                if (s[1] > 0 && cnt[s[1] - 1][s[0]] == 0 && S[s[1] - 1][s[0]] == '.') {
                    cnt[s[1] - 1][s[0]] = cnt[s[1]][s[0]] + 1;
                    que.add(new int[]{s[0], s[1] - 1});
                }
                if (s[1] < H - 1 && cnt[s[1] + 1][s[0]] == 0 && S[s[1] + 1][s[0]] == '.') {
                    cnt[s[1] + 1][s[0]] = cnt[s[1]][s[0]] + 1;
                    que.add(new int[]{s[0], s[1] + 1});
                }
            }

            int[] ret = new int[3];
            for (int i = 0; i < H; i++) {
                for (int j = 0; j < W; j++) {
                    if (ret[2] < cnt[i][j]) {
                        ret[0] = j;
                        ret[1] = i;
                        ret[2] = cnt[i][j];
                    }
                }
            }
            return ret;
        }

    }

    static class MyScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public MyScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    String rl = in.readLine();
                    if (rl == null) {
                        return null;
                    }
                    st = new StringTokenizer(rl);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}