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: Accepted)

import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static boolean visited[][];
    static int[][] s;
    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        int H = scan.nextInt();
        int W = scan.nextInt();
        s = new int[H][W];
        for(int i = 0; i < H; i++) {
            String tmp = scan.next();
            for(int j = 0; j < W; j++){
                if(tmp.charAt(j) == '.')
                    s[i][j] = 0;
                else
                    s[i][j] = 1;
            }
        }

        int ans = 0;
        for(int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                for(int k = i; k < H; k++) {
                    int l;
                    if(k == i)
                        l = j+1;
                    else
                        l = 0;
                    for (; l < W; l++) {
                        if(s[i][j] != 0 || s[k][l] != 0) continue;
                        visited = new boolean[H][W];
//                        System.out.println("start " + i + " " + j + " end " + k + " " + l);
                        int level = bfs(new int[]{i,j}, new int[]{k,l});
//                        System.out.println("level " + level);
                        ans = Math.max(ans, level);
                    }
                }
            }
        }

        os.println(ans);
    }

    static int[][] dirs={{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

    public static int bfs(int[] start, int[] end){
        visited[start[0]][start[1]] = true;
        int level = 0;
        Queue<int[]> queue = new LinkedList<>();
        queue.add(start);
        while (!queue.isEmpty()) {
            Queue<int[]> queue2 = new LinkedList<>();
            while (!queue.isEmpty()) {
                int[] p = queue.remove();
                if (p[0] == end[0] && p[1] == end[1])
                    return level;
                for (int[] dir : dirs) {
                    int x = p[0] + dir[0];
                    int y = p[1] + dir[1];
                    if (x >= 0 && y >= 0 && x < s.length && y < s[0].length && s[x][y] == 0) {
                        if (!visited[x][y]) {

                            queue2.add(new int[]{x, y});
                            visited[x][y] = true;
                        }
                    }
                }
            }
            queue = queue2;
            level++;
        }

        return level;
    }
}