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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
	static int n;
	static int h;
	static int w;

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

	public static void main(String args[]) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");
		h = Integer.parseInt(str[0]);
		w = Integer.parseInt(str[1]);

		String matrix[][] = new String[h][w];

		for (int i = 0; i < h; i++) {
			str = br.readLine().split("");
			for (int j = 0; j < w; j++) {
				matrix[i][j] = str[j];
			}
		}

		int max = 0;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				int dist[][] = new int[h][w];
				if (matrix[i][j].equals(".")) {
					dist = bfs(matrix, j, i);
				}
				for (int k = 0; k < h; k++) {
					for (int l = 0; l < w; l++) {
						max = Math.max(max, dist[k][l]);
					}
				}
			}
		}
		System.out.println(max);
	}

	static int[][] bfs(String matrix[][], int sx, int sy) {
		int dist[][] = new int[h][w];
		boolean used[][] = new boolean[h][w];

		Queue<Integer> qX = new ArrayDeque<>();
		Queue<Integer> qY = new ArrayDeque<>();
		qX.add(sx);
		qY.add(sy);
		used[sy][sx] = true;
		dist[sy][sx] = 0;

		while (!qX.isEmpty() && !qY.isEmpty()) {
			int x = qX.poll();
			int y = qY.poll();

			for (int i = 0; i < 4; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];

				if (nx >= 0 && nx < w && ny >= 0 && ny < h && matrix[ny][nx].equals(".")) {
					if (!used[ny][nx]) {
						used[ny][nx] = true;
						dist[ny][nx] = dist[y][x] + 1;
						qX.add(nx);
						qY.add(ny);
					}
				}
			}
		}
		return dist;
	}
}