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: Accepted)

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pi pair<ll,ll>
#define all(X) X.begin(),X.end()

// for pair comparison function(ascending order) use return (i1.ff < i2.ff);

/* string operations :
   str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
   str.substr (x) : returns a substring str[x],... end of string
   str.find(qtr) : returns the first occurenece of qtr in str */

const int M = (1<<20)+5;
const int md = 1e9+7;

ll pwr(ll a,ll n,ll m)
{
	ll p=1;
	while(n>0)
	{
		if(n%2==1)
			p=(p*a)%m;
		a=(a*a)%m;
		n=n/2;
	}
	return p;
}

string str[500];
ll far[500][500],mrk[500][500],n,m;

ll get(ll x,ll y)
{
	if(x>=1 && x<=n && y>=1 && y<=m && str[x][y-1] == '.')
		return 1;
	else
		return 0;
}

queue<pi> pq;

ll check(ll p,ll q)
{

	ll i,j,x,y,ans = 0;

	if(str[p][q-1] == '#')
		return 0;

	for(i=1;i<=n;++i)
		for(j=1;j<=m;++j)
		{
			far[i][j] = 1e9;
			mrk[i][j] = 0;
		}

	far[p][q] = 0;
	x = p;
	y = q;

	pq.push(pi(p,q));
	while(!pq.empty())
	{
		p = (pq.front()).ff;
		q = (pq.front()).ss;
		pq.pop();
		
		if(mrk[p][q])
			continue;
		
		ans = max(ans,far[p][q]);

		mrk[p][q] = 1;
		if(get(p+1,q))
		{
			far[p+1][q] = min(far[p+1][q],far[p][q]+1);
			if(far[p+1][q] == far[p][q] + 1)
				pq.push(pi(p+1,q));
		}
		if(get(p-1,q))
		{
			far[p-1][q] = min(far[p-1][q],far[p][q]+1);
			if(far[p-1][q] == far[p][q] + 1)
				pq.push(pi(p-1,q));
		}
		if(get(p,q+1))
		{
			far[p][q+1] = min(far[p][q+1],far[p][q]+1);
			if(far[p][q+1] == far[p][q] + 1)
				pq.push(pi(p,q+1));
		}
		if(get(p,q-1))
		{
			far[p][q-1] = min(far[p][q-1],far[p][q]+1);
			if(far[p][q-1] == far[p][q] + 1)
				pq.push(pi(p,q-1));
		}
	}

	return ans;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	ll i,j,ans = 0;

	cin >> n >> m;

	for(i=1;i<=n;++i)
	{
		cin >> str[i];
	}

	for(i=1;i<=n;++i)
	{
		for(j=1;j<=m;++j)
			ans = max(ans,check(i,j));
	}

	cout << ans << "\n";
	return 0;
}