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 JavaScript (node.js v5.12) (judge verdict: Accepted)

function Main(input) {
  input = input.split("\n");
  var H = parseInt(input[0].split(" ")[0]);
  var W = parseInt(input[0].split(" ")[1]);
  var mod = Math.pow(10,9)+7;
  input.shift();
  input.splice(H,input.length);
  var S = input.map(i => i.split(""));
  var edge = new Array();
  var id=0;
  for(var i=0;i<H;i++){
    for(var j=0;j<W;j++){
      if(S[i][j]=="."){
        if(i!=H-1){
          if(S[i+1][j]=="."){
            edge.push([id,id+W]);
          }
        }
        if(j!=W-1){
        if(S[i][j+1]=="."){
            edge.push([id,id+1]);
          }
        }
      }
      id++;
    }
  }
  var map = new Array(H*W);
  for(var i=0;i<H*W;i++){
    var mapsub = new Array(H*W).fill(mod);
    mapsub[i]=0;
    map[i]=mapsub;
  }
  for(var i=0;i<edge.length;i++){
    map[edge[i][0]][edge[i][1]]=1;
    map[edge[i][1]][edge[i][0]]=1;
  }
  var ans =0;
  for(var k=0;k<H*W;k++){
    for(var i=0;i<H*W;i++){
      for(var j=0;j<H*W;j++){
        map[i][j]=Math.min(map[i][k]+map[k][j],map[i][j]);
      }
    }
  }
  for(var i=0;i<H*W;i++){
    for(var j=0;j<H*W;j++){
      if(map[i][j]<mod){
        ans=Math.max(ans,map[i][j]);
      }
    }
  }

  console.log('%s', ans);
  return ans;
}

function debug(n){
  var input = document.getElementById("input"+n).value;
  var ans=Main(input);
  var result="WA";
  if(ans==document.getElementById("output"+n).value.split("\n")[0]){
    result="AC";
  }
  document.getElementById("result"+n).innerHTML=result;
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));