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 fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define INF 10000000001
#define DIM 200001
#define mod 1000000007
#define PI 3.1415926535897932384626433
#define se second
#define fi first
#define pb push_back
#define T true
#define F false
#define endl "\n"
typedef char ch;
typedef bool boll;
typedef string st;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef set < string > sst;
typedef set < long long > sl;
typedef queue < long long > que;
typedef vector < long long > vec;
typedef map < long long, long long > mll;
typedef pair < long long, long long > pll;
typedef vector < pair < long long, long long > > vpl;
typedef pair < long long, unsigned long long > pul;
ll n,m,z,x,c,s,maxs=-1,max2=-1,a[51][51],vis[21][21],x2,y2;
pll b[501];
void dfs(long long x,long long y,long long sum)
{
    if(a[x][y]==-1 || x<1 || x>n || y<1 || y>m || vis[x][y]==1)return;
    else
    {
        vis[x][y]=1;
        if(maxs<sum)
        {
            maxs=sum;
            x2=x;
            y2=y;
        }
        dfs(x+1,y,sum+1);
        dfs(x-1,y,sum+1);
        dfs(x,y+1,sum+1);
        dfs(x,y-1,sum+1);
    }

}
void dfs2(long long x,long long y,long long sum)
{
   // if(x==x2 && y==y2)max2=min(max2,sum);
    if(x<1 || x>n || y<1 || y>m || a[x][y]==-1)return;
    if(sum<vis[x][y])
    {
        vis[x][y]=sum;
        dfs2(x+1,y,sum+1);
        dfs2(x-1,y,sum+1);
        dfs2(x,y+1,sum+1);
        dfs2(x,y-1,sum+1);
    }
}
int main()
{
    fast;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            ch cc;
            cin>>cc;
            if(cc=='#')a[i][j]=-1;
            else
            {
                z++;
                b[z]= {i,j};
            }
        }
    }
   // max2=INF;
    dfs(b[1].fi,b[1].se,0);
    for(int x1=1; x1<=n; x1++)
        for(int x2=1; x2<=m; x2++)vis[x1][x2]=INF;
    dfs2(x2,y2,0);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)if(vis[i][j]!=INF)max2=max(max2,vis[i][j]);
    maxs=-1;
    for(int x1=1; x1<=n; x1++)
        for(int x2=1; x2<=m; x2++)vis[x1][x2]=INF;
    dfs2(b[1].fi,b[1].se,0);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)if(vis[i][j]!=INF)maxs=max(maxs,vis[i][j]);
    cout<<max(max2,maxs)<<endl;
}