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.util.*;
import java.io.PrintWriter;
import static java.lang.Integer.*;
import static java.lang.Long.*;
import static java.lang.Math.*;
import static java.lang.System.*;

public class Main {
	public static ArrayDeque<Point> que;
	public static int[][] ll;
	public static char[][] s;
	public static int h;
	public static int w;
	public static int[] xadd = {-1, 1, 0, 0}; 
	public static int[] yadd = {0, 0, -1, 1}; 
	public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		h = parseInt(sc.next());
		w = parseInt(sc.next());
		s = new char[h][];
		for(i=0;i<h;i++) {
			s[i] = sc.next().toCharArray();
		}
		sc.close();
		int mx=0;
		for (int hs = 0; hs < h; hs++) {
			for (int ws = 0; ws < w; ws++) {
				if(s[hs][ws]=='#') continue;
				for (int he = 0; he < h; he++) {
					for (int we = 0; we < w; we++) {
						if(s[he][we]=='#') continue;
						if(hs==he&&ws==we) continue;
						que = new ArrayDeque<>();
						ll = new int[h][w];
						for (i = 0; i < h; i++) {
							Arrays.fill(ll[i], 401);
						}
						ll[hs][ws] = 0;
						que.add(new Point(hs, ws));
//						out.printf("%d,%d -> %d,%d%n", hs, ws,he, we);
						while (que.size() > 0) {
							Point p = que.removeFirst();
							if(p.h == he && p.w == we) break;
							for (i=0; i<4; i++) {
								int ww = p.w+xadd[i];
								int hh = p.h+yadd[i];
								if(ww<0||ww>=w) continue;
								if(hh<0||hh>=h) continue;
								if (s[hh][ww] == '.'&&ll[hh][ww]==401) {
									que.addLast(new Point(hh, ww));
									ll[hh][ww] = ll[p.h][p.w] + 1;
//									out.printf("%d,%d%n",p.h,p.w);
//									out.println(Arrays.deepToString(ll));
								}
							}
						}
						int l = ll[he][we];
						if(mx<l) mx=l;
//						out.println(Arrays.deepToString(ll));
//						out.printf("%d%n", l);
						que = null;
						ll = null;
					}
				}
			}
		}
		out.println(mx);
	}
}
class Point {
	int h;
	int w;
	Point(int h, int w) {
		this.h = h;
		this.w = w;
	}
	
}