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>
using namespace std;
#define ll long long
#define mod 1000000007ll
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define db(x) cout << #x << " = " << x << '\n'
#define sep cout << "********************************************\n"
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
ll n, m;
ll dx[4] = {0, -1, 0, 1};
ll dy[4] = {-1, 0, 1, 0};
string grid[25];
ll vis[25][25];
bool valid(ll xx, ll yy)
{
	if (xx < 0 || yy < 0)
		return false;
	if (xx >= n || yy >= m)
		return false;
	if (grid[xx][yy] == '#')
		return false;
	if (vis[xx][yy] != -1)
		return false;
	return true;
}
ll bfs(ll sx, ll sy)
{
	
	queue<pll> q;
	q.push(mp(sx, sy));
	vis[sx][sy] = 0;
	while (!q.empty())
	{
		pll fr = q.front();
		q.pop();
		for (int i = 0; i < 4; ++i)
		{
			if (valid(fr.x + dx[i], fr.y + dy[i]))
			{
				q.push(mp(fr.x + dx[i], fr.y + dy[i]));
				vis[fr.x + dx[i]][fr.y + dy[i]] = 1 + vis[fr.x][fr.y];
			}
		}
	}
	ll ans = 0;
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			ans = max(ans, vis[i][j]);
		}
	}
	memset(vis, -1, sizeof(vis));
	return ans;
}
int main(int argc, char const *argv[])
{
	fast;
	memset(vis, -1, sizeof(vis));
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	{
		cin >> grid[i];
	}
	ll ans = 0;
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			if (valid(i, j))
				ans = max(ans, bfs(i, j));
		}
	}
	cout << ans;
	return 0;
}