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;
typedef long long int ll;
#define rep( i, n, m ) for(int i = ( n ); i < ( m ); i++)
#define rep_d( i, n, m ) for(int i = ( n ) - 1; i >= ( m ); i--)
#define sort_asc( X ) sort(( X ).begin(), ( X ).end())
#define sort_desc( X ) sort(( X ).begin(), ( X ).end(), greater <>())
#define P pair <int, int>

template <class T> bool chmax( T& a, T b );
template <class T> bool chmin( T& a, T b );
int* eratosthenes( int N );
bool bit_search( int pattern, int N );
template <class T> void cumulative_sum( T array, size_t N );

const ll INF = 1LL << 60;
const int k_mod = 1e9 + 7;

int H, W;
int cnt = 0;
void calc( vector <vector <bool> >& vec, int i, int j, vector <vector <int> >& dist, int n ){
	if (i == H || i == -1 || j == -1 || j == W)
		return;
	cnt++;
	if (vec[ i ][ j ] && ( dist[ i ][ j ] > n || dist[ i ][ j ] < 0 )){
		dist[ i ][ j ] = n;
		n++;
		calc( vec, i + 1, j, dist, n );
		calc( vec, i, j + 1, dist, n );
		calc( vec, i - 1, j, dist, n );
		calc( vec, i, j - 1, dist, n );
	}
}

int main( void ){
	cin >> H >> W;
	vector <vector <bool> > vec( H, vector <bool>( W ));
	rep( i, 0, H ){
		rep( j, 0, W ){
			char str;
			cin >> str;
			if (str == '.')
				vec[ i ][ j ] = true;
			else
				vec[ i ][ j ] = false;
		}
	}

	int maximum = 0;
	vector <vector <int> > dist( H, vector <int>( W ));
	queue <P> q;
	int v[ 4 ][ 2 ] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
	rep( i, 0, H ){
		rep( j, 0, W ){
			if (!vec[ i ][ j ])
				continue;
			rep( k, 0, H )
			rep( l, 0, W )
			dist[ k ][ l ] = -1;
			dist[ i ][ j ] = 0;
			q.push( P( i, j ));
			int route = 0;
			while(!q.empty()){
				P now = q.front(); q.pop();
				int d = dist[ now.first ][ now.second ] + 1;
				rep( i, 0, 4 ){
					int x = now.first + v[ i ][ 0 ], y = now.second + v[ i ][ 1 ];
					if (x == H || x == -1 || y == -1 || y == W)
						continue;
					if (vec[x][y] && dist[ x ][ y ] == -1){
						dist[ x ][ y ] = d;
						route = max( route, d );
						q.push( P( x, y ));
					}
				}
			}
			maximum  = max( maximum, route );
		}
	}
	cout << maximum << endl;
}

int* eratosthenes( int N ){
	int* prime_array = new int[ N + 1 ];
	int lim = N;
	prime_array[ 0 ] = 0;
	prime_array[ 1 ] = 0;
	rep( i, 2, N + 1 ){
		prime_array[ i ] = 1;
	}
	rep( i, 2, lim ){
		if (prime_array[ i ] == 0)
			continue;
		lim = N / i;
		for(int j = i * 2; j < N + 1; j += i){
			prime_array[ j ] = 0;
		}
	}
	return prime_array;
}
template <class T> void cumulative_sum( T array, size_t N ) {
	rep( i, 1, N + 1 ){
		array[ i ] += array[ i - 1 ];
	}
}
template <class T> bool chmax( T& a, T b ) {
	if (a < b){
		a = b; return true;
	}
	return false;
}
template <class T> bool chmin( T& a, T b ) {
	if (a > b){
		a = b; return true;
	}
	return false;
}
bool bit_search( int pattern, int N ){
	int cnt = 0;
	rep( bit, 0, N ){
		if (pattern & ( 1 << bit )){
			cnt++;
		}
	}
	return true;
}