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 idfc ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define ll long long
#define pb push_back
#define mp make_pair
#define nt _int128
#define ld long double
long double PI =3.14159265358979323846;


const ll modo=1e9+7;
const ll inf=2e14;
const ll ms=(1e5) +5;

int x,y;
int ar[30][30];
int vis[30][30];
int dis[30][30];

bool check(int i,int j,int a,int b)
{
    if(i>=1&&i<=x&&j>=1&&j<=y&&!vis[i][j]&&ar[i][j])return true;
    if(i>=1&&i<=x&&j>=1&&j<=y&&vis[i][j]&&ar[i][j]&&(dis[i][j]>(dis[a][b]+1)))return true;
    return false;
}

int func(int i,int j,int a,int b)
{
    vis[i][j]=1;
    dis[i][j]=dis[a][b]+1;
    return dis[i][j];
}

int callme(int i,int j)
{
   queue<pair<int,int>> qq;
   qq.push({i,j});
   int a,b;
   int mx=0;
   while(!qq.empty())
   {
       auto it=qq.front();
       qq.pop();
       a=it.first;
       b=it.second;
       if(check(a-1,b,a,b))
       {
           mx=max(func(a-1,b,a,b),mx);
           qq.push({a-1,b});
       }
       if(check(a+1,b,a,b))
       {
           mx=max(mx,func(a+1,b,a,b));
           qq.push({a+1,b});
       }
       if(check(a,b-1,a,b))
       {
           mx=max(mx,func(a,b-1,a,b));
           qq.push({a,b-1});
       }
       if(check(a,b+1,a,b))
       {
           mx=max(mx,func(a,b+1,a,b));
           qq.push({a,b+1});
       }
   }
   return mx;
}



int main()
{
    idfc;
    cin>>x>>y;
    int i;int j;
    char e;
    for(i=1;i<=x;i++)
    {
        for(j=1;j<=y;j++)
        {
           cin>>e;
           if(e=='.')ar[i][j]=1;
           else ar[i][j]=0;
        }
    }
    int ans=0;
    for(i=1;i<=x;i++)
    {
        for(j=1;j<=y;j++)
        {
            if(ar[i][j])
            {
                memset(vis,0, sizeof(vis));
                memset(dis,0, sizeof(dis));
                ans=max(ans,callme(i,j));
            }
        }
    }
    cout<<ans;
    return 0;
}