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.awt.Point;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	static final char WALL = '#';
	static final int ROAD = '.';
	static char[][] board;

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] tmpArray = br.readLine().split(" ");
		int h = Integer.parseInt(tmpArray[0]);
		int w = Integer.parseInt(tmpArray[1]);
		
		board = new char[h][w];
		
		for(int i = 0; i < h; i++) {
			String str = br.readLine();
			
			for(int j = 0; j < w; j++) {
				board[i][j] = str.charAt(j);
			}
		}
		
		int result = 0;
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				for(int k = 0 ; k < h ; k++) {
					for(int l = 0; l < w; l++) {
						result = Math.max(result, solve(j, i, l, k));					
					}
				}
			}
		}
		
		System.out.println(result);
	}

	static final int dx[] = {0,1,0,-1};
	static final int dy[] = {-1,0,1,0};
	
	static int solve(int startX, int startY, int goalX, int goalY) {

		if(board[startY][startX] == WALL || board[goalY][goalX] == WALL) { 
			return -1;
		}
//		System.out.println("from ("+startX+" "+startY+") to ("+goalX+" "+goalY+")");
		ArrayList<Point> queue = new ArrayList<Point>();
		int h = board.length;
		int w = board[0].length;
		
		int dist[][] = new int[h][w];
		
		dist[startY][startX] = 0;
		
		queue.add(new Point(startX, startY));
		
		int maxLen = 0;
		while(!queue.isEmpty()) {
			Point p = queue.remove(0);
			
			if(p.x == goalX && p.y == goalY) {
				break;
			}
			
			for(int i = 0; i < 4; i++) {
				int tmpX = p.x + dx[i];
				int tmpY = p.y + dy[i];
				
				if(tmpX >= 0 && tmpX < w && tmpY >= 0 && tmpY < h) {
					if(board[tmpY][tmpX] == ROAD && dist[tmpY][tmpX] == 0) {
						dist[tmpY][tmpX] = dist[p.y][p.x] + 1;
						maxLen = Math.max(maxLen, dist[tmpY][tmpX]);
						queue.add(new Point(tmpX, tmpY));
					}
				}
			}
//			printArray(dist);
		}
		return maxLen;
	}
	
	static void printArray(int[][] array) {
		for(int i = 0; i < array.length; i++) {
			for(int j = 0; j < array[0].length; j++) {
				System.out.print(array[i][j]+" ");
			}
			System.out.println();
		}
	}
}