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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007
#define pb push_back
#define vl vector<ll>
#define pll pair<ll,ll>
#define mp make_pair
#define fi first
#define se second
# define ld long double
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int ma=1e5+5;
int h,w;
char arr[25][25];
int bfs(int p,int q)
{
  vector<vector<int> >dis(h);
  for(int i=0;i<h;i++)
  {
    dis[i].resize(w);
    for(int j=0;j<w;j++)
    dis[i][j]=0;
  }

  queue< pair<int,int> >Q;
  Q.push({p,q});
  int mx=-1;

  while(!Q.empty())
  {
    pair<int,int>pt=Q.front();
    //cout<<pt.fi<<" "<<pt.se<<endl;
    Q.pop();

      if(pt.fi-1>=0 && dis[pt.fi-1][pt.se]==0 && arr[pt.fi-1][pt.se]=='.' )
      {
        dis[pt.fi-1][pt.se]=dis[pt.fi][pt.se]+1;
        Q.push({pt.fi-1,pt.se});
        mx=max(mx,dis[pt.fi-1][pt.se]);
      }

      if(pt.fi+1<h && dis[pt.fi+1][pt.se]==0 && arr[pt.fi+1][pt.se]=='.' )
      {
        dis[pt.fi+1][pt.se]=dis[pt.fi][pt.se]+1;
        Q.push(mp(pt.fi+1,pt.se));
        mx=max(mx,dis[pt.fi+1][pt.se]);
      }

      if( pt.se-1>=0 && dis[pt.fi][pt.se-1]==0 && arr[pt.fi][pt.se-1]=='.')
      {
        dis[pt.fi][pt.se-1]=dis[pt.fi][pt.se]+1;
        Q.push({pt.fi,pt.se-1});
        mx=max(mx,dis[pt.fi][pt.se-1]);
      }

      if(pt.se+1<w && dis[pt.fi][pt.se+1]==0 && arr[pt.fi][pt.se+1]=='.' )
      {
        dis[pt.fi][pt.se+1]=dis[pt.fi][pt.se]+1;
        Q.push({pt.fi,pt.se+1});
        mx=max(mx,dis[pt.fi][pt.se+1]);
      }
  }
return mx;
}
int main()
{
  cin>>h>>w;
  int mx=0;

  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
    cin>>arr[i][j];

  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
    {
      if(arr[i][j]=='.')
        mx=max(mx,bfs(i,j));
    }
    cout<<mx<<endl;
  return 0;
}