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: Wrong Answer)

function Main(input) {
  input = input.split("\n");
  var H = parseInt(input[0].split(" ")[0]);
  var W = parseInt(input[0].split(" ")[1]);
  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 queue=new Array();
  var save = new Array(H*W).fill(0);
  var dist = new Array(H*W);
  var d =0;
  save[0]=1;
  queue.push([0,0]);
  var start=-1;
  while(queue.length!=0&&start==-1){
    var now =queue[0][0];
    var d =queue[0][1];
    dist[now]=[d,0];
    queue.shift();
    var flag=0;
    for(var i=0;i<edge.length;i++){
      if(edge[i][0]==now&&save[edge[i][1]]==0){
        queue.push([edge[i][1],d+1]);
        save[edge[i][1]]=1;
        flag=1;
      }
      if(edge[i][1]==now&&save[edge[i][0]]==0){
        queue.push([edge[i][0],d+1]);
        save[edge[i][0]]=1;
        flag=1;
      }
    }
    if(flag!=1){
      start=now;
    }
  }

  var queue=new Array();
  var save = new Array(H*W).fill(0);
  var dist = new Array(H*W);
  var d =0;
  save[start]=1;
  var ans =0;
  queue.push([start,0]);
  while(queue.length!=0){
    var now =queue[0][0];
    var d =queue[0][1];
    dist[now]=[d,0];
    ans = Math.max(ans,d);
    queue.shift();
    var flag=0;
    for(var i=0;i<edge.length;i++){
      if(edge[i][0]==now&&save[edge[i][1]]==0){
        queue.push([edge[i][1],d+1]);
        save[edge[i][1]]=1;
        flag=1;
      }
      if(edge[i][1]==now&&save[edge[i][0]]==0){
        queue.push([edge[i][0],d+1]);
        save[edge[i][0]]=1;
        flag=1;
      }
    }
    if(flag!=1){
      dist[now][1]=1;
    }
  }


  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"));