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: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265359
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl '\n'
ll power(ll x, ll y, ll p)
{ 
    ll res = 1;  
    x = x % p;
    while (y > 0) { 
        if (y & 1) res = (res * x) % p; 
        y = y >> 1;
        x = (x * x) % p;   
    } 
    return res; 
}
const ll L = 40;
ll grid[L][L];
ll vis[L][L];
ll h, w;
pll src, trg;
struct node
{	
	ll x, y, dist;
};
ll bfs() {
	deque<node> q;
	node temp;
	if (src == trg) return 0;
	temp.x = src.fi;
	temp.y = src.se;
	temp.dist = 1;
	q.push_front(temp);
	while(!q.empty())
	{
		node p = q.front();
		q.pop_front();
		ll i;
		vis[p.x][p.y] = 1;
		vector <node> vec;
		node p1 = p;
		p1.x -= 1;
		node p2 = p;
		p2.x += 1;
		node p3 = p;
		p3.y -= 1;
		node p4 = p;
		p4.y += 1;
		vec.pb(p1);
		vec.pb(p2);
		vec.pb(p3);
		vec.pb(p4);
		for (ll i = 0; i < 4; i++) {
			if (vec[i].x == trg.fi && vec[i].y == trg.se) {
 				return vec[i].dist;
			}
			if (vec[i].x < 0 || vec[i].y < 0 || vec[i].x >= w || vec[i].y >= h) {
				continue;
			}
			if (vis[vec[i].x][vec[i].y] == 1 || grid[vec[i].x][vec[i].y] == 0) continue;
			vec[i].dist += 1;
			q.push_back(vec[i]);
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    // cout.tie(NULL);
 	cin >> h >> w;
 	ll ans = 0;
 	for (ll i = 0; i < h; i++) {
 		string s;
 		cin >> s;
 		for (ll j = 0; j < w; j++) {
 			if (s[j] == '.') {
 				grid[j][i] = 1;
 			}
 		}
 	}
 	for (ll x1 = 0; x1 < w; x1++) {
 		for (ll y1 = 0; y1 < h; y1++) {
 			for (ll x2 = 0; x2 < w; x2++) {
 				for (ll y2 = 0; y2 < h; y2++) {
 					if (grid[x1][y1] == 1 && grid[x2][y2] == 1) {
	 					for (ll i = 0; i < w; i++) {
	 						for (ll j = 0; j < h; j++) {
	 							vis[i][j] = 0;
	 						}
	 					}
	 					src = {x1, y1};
	 					trg = {x2, y2};
	 					ll pos = bfs();
 						ans = max(ans, pos);	
 					}
 				}
 			}
 		}
 	}   
 	cout << ans << endl;
	return 0;
}