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.util.Map.Entry;
 
 
 class Main {
	 static int mod =  (int) (Math.pow(10,9)+7);
	 static List<ArrayList<Integer>>  list = new ArrayList<ArrayList<Integer>>();
	 static char[][] S;
    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        int H = sc.nextInt();
        int W = sc.nextInt();
        S = new char[H][W];
        
        
        for(int i=0;i<H;i++){
        	String s=sc.next();
        	S[i] = s.toCharArray();
        }
        
        int max=0;
        for(int i=0;i<H*W;i++) {
        	max=Math.max(max, kaisi(i,H,W));
        }
//        max=Math.max(max, kaisi(0,H,W));
//        max=Math.max(max, kaisi(W-1,H,W));
//        max=Math.max(max, kaisi((H-1)*W,H,W));
//        max=Math.max(max, kaisi((H-1)*W+W-1,H,W));
                               
        System.out.println(max);        	
                		
    }
    
    public static int kaisi(int k,int H,int W) {
        int max = 0;
        
        boolean[][] visited = new boolean[H][W];
        LinkedList<Integer> queue = new LinkedList<Integer>();
        
        int kh=k/W;
        int kw=k%W;
        if(S[kh][kw]=='#')return -1;
        visited[kh][kw]=true;
        queue.add(k);
        queue.add(0);
        while(!queue.isEmpty()) {
        	int now = queue.poll();
        	int d = queue.poll();
        	max=Math.max(max, d);
        	int nowh=now/W;
        	int noww=now%W;
        	if(kensa(nowh,noww+1,H,W,visited)==1) {
        		queue.add(nowh*W+noww+1);
        		queue.add(d+1);
        		visited[nowh][noww+1]=true;
        	}
        	if(kensa(nowh,noww-1,H,W,visited)==1) {
        		queue.add(nowh*W+noww-1);
        		queue.add(d+1);
        		visited[nowh][noww-1]=true;
        	}
        	if(kensa(nowh+1,noww,H,W,visited)==1) {
        		queue.add((nowh+1)*W+noww);
        		queue.add(d+1);
        		visited[nowh+1][noww]=true;
        	}
        	if(kensa(nowh-1,noww,H,W,visited)==1) {
        		queue.add((nowh-1)*W+noww);
        		queue.add(d+1);
        		visited[nowh-1][noww]=true;
        	}
        }
        return max;
    }
    
    public static int kensa(int nowh,int noww,int H,int W,boolean[][] v) {
    	if(nowh<0||nowh>=H||noww<0||noww>=W) {
    		return -1;
    	}
    	if(v[nowh][noww]==true||S[nowh][noww]=='#') {
    		return -1;
    	}
    	
    	return 1;
    }
    
}