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.util.*;
public class Main{   
    public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      int h = sc.nextInt();
      int w = sc.nextInt();
      char[][] c = new char[h][w];
      int INF = 1000000;
      for(int i=0;i<h;i++)c[i]=sc.next().toCharArray();
      int[][] path = new int[h*w+1][h*w+1];
      for(int i=0;i<=h*w;i++)Arrays.fill(path[i],INF);
      for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
          if(c[i][j]=='#'){
            if(i<h-1){
              path[i*w+j][(i+1)*w+j]=INF;
              path[(i+1)*w+j][i*w+j]=INF;
            }
            if(j<w-1){
              path[i*w+j][i*w+j+1]=INF;
              path[i*w+j+1][i*w+j]=INF;
            }
            if(i>0){
              path[i*w+j][(i-1)*w+j]=INF;
              path[(i-1)*w+j][i*w+j]=INF;
            }
            if(j>0){
              path[i*w+j][i*w+j-1]=INF;
              path[i*w+j-1][i*w+j]=INF;
            }
          }else{
            if(i<h-1&&c[i+1][j]=='.'){
              path[i*w+j][(i+1)*w+j]=1;
              path[(i+1)*w+j][i*w+j]=1;
            }
            if(j<w-1&&c[i][j+1]=='.'){
              path[i*w+j][i*w+j+1]=1;
              path[i*w+j+1][i*w+j]=1;
            }
            if(i>0&&c[i-1][j]=='.'){
              path[i*w+j][(i-1)*w+j]=1;
              path[(i-1)*w+j][i*w+j]=1;
            }
            if(j>0&&c[i][j-1]=='.'){
              path[i*w+j][i*w+j-1]=1;
              path[i*w+j-1][i*w+j]=1;
            }
          }
        }
      }
      int max = 0;
      for(int i=0;i<h*w;i++)path[i][i]=INF;
      for(int i=0;i<h*w;i++)
        for(int j=0;j<h*w;j++){
          if(i==j)continue;
          if(i>0)path[i][j]=Math.min(path[i][j],path[i-1][j]+1);
          if(j>0)path[i][j]=Math.min(path[i][j],path[i][j-1]+1);
          if(i<h-1)path[i][j]=Math.min(path[i][j],path[i+1][j]-1);
          if(j<w-1)path[i][j]=Math.min(path[i][j],path[i][j+1]-1);
        }
      List<Integer> list = new ArrayList<>();
      for(int i=0;i<h*w;i++)
        for(int j=0;j<h*w;j++)if(path[i][j]<=1000&&i!=j)list.add(path[i][j]);
      Collections.sort(list);
      System.out.println(list.get(list.size()-1));
    }
    public static int lcm(int x, int y){
      return x*y/gcd(x,y);
    }
    public static int gcd(int x, int y){
      if(x < y)return gcd(y,x);
      if(y==0)return x;
      return gcd(y,x%y);
    }
}
class Pair{
    int a,b;

    Pair(int a, int b){
      this.a = a;
      this.b = b;
    }
}