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.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {

	//4方向の場合は0～5,8方向は0～9でループ
	static int[] around = { -1, 0, 1, 0, -1, 1, 1, -1, -1 };

	public static void main(String[] args) throws Exception {
		// TODO 自動生成されたメソッド・スタブ
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String W = in.readLine();

		int HI = Integer.parseInt(W.split(" ")[0])+2;
		int WI = Integer.parseInt(W.split(" ")[1])+2;

		boolean[][] S = new boolean[HI][WI] ;
		for(int i=0;i<S.length;i++){
			Arrays.fill(S[i], false);
		}
		for(int i=0;i<S.length-2;i++){
			W = in.readLine();
			for(int k=0;k<W.length();k++){
				if('.' == W.charAt(k)){
					S[i+1][k+1] = true;
				}else{
					S[i+1][k+1] = false;
				}
			}
		}
		int ans = -1;
		for(int w=0;w<S.length;w++){
			for(int h=0;h<S[0].length;h++){
				if(!S[w][h]){
					continue;
				}
				boolean[][] tmpS = new boolean[HI][WI] ;
				for(int i=0;i<S.length;i++){
					tmpS[i] = Arrays.copyOf(S[i], S[i].length);
				}
				//ある道から幅優先で探索し経路の長さを全探索する
			 	ans = Math.max(BWS(w, h,tmpS),ans);
			}
		}

		System.out.println(ans);
	}

	public static int BWS(int w , int h,boolean[][] S){
		int len = 0;
		ArrayList<int[]> queue = new ArrayList<int[]>();
		int[] p = {w,h};
		queue.add(p);
		//現在地は壁に変更する
		S[p[0]][p[1]]=false;

		while(queue.size()!=0){
			ArrayList<int[]> queue2 = new ArrayList<int[]>();
			while(queue.size()!=0){
				p = queue.get(0);
				queue.remove(0);

				//pの上下左右で道があれば追加
				for(int i=0;i<5;i++){
					int[] aftP = {p[0]+around[i],p[1]+around[i+1]};
					if(S[aftP[0]][aftP[1]]){
						S[aftP[0]][aftP[1]]=false;
						queue2.add(aftP);
					}
				}
			}
			if(queue2.size()!=0){
				len++;
				queue=queue2;
			}
//			System.out.println(" ");
//		print(S);
		}

		return len;
	}

	public static void print(boolean[][] S){
		for(int i=0;i<S.length;i++){
			for(int k=0;k<S[0].length;k++){
				if(S[i][k]){
					System.out.print(". ");
				}else{
					System.out.print("# ");
				}
			}
			System.out.println("");
		}
	}
}