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)

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

template <class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_mset;

#define FOR(i,a,b) for(int i = a; i < b; i++)
#define F0R(i,a) for(int i = 0; i < a; i++)
#define F0Rd(i,a) for (int i = a - 1; i >=0 ; i--)
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) int((x).size())
#define newl '\n'

const int MXN = 1e5 + 5;
const ll MOD = 998244353;
const ll INF = 1000000007;


const int inf = 1000000007;

int H, W, dist[405][405], adj[405][405];
char grid[25][25];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

bool valid(int x, int y){
	return 1 <= x && x <= H && 1 <= y && y <= W;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	//ifstream cin("/Users/konwoo/input.txt");
	//ofstream cout("/Users/konwoo/output.txt");

	cin >> H >> W;
	F0R(i, 25){
		F0R(j, 25){
			grid[i][j] = '#';
		}
	}
	F0R(i,H){
		F0R(j,W){
			cin >> grid[i+1][j+1];
		}
	}
	F0R(i, H){
		F0R(j, W){
			if (grid[i+1][j+1] == '.'){
				F0R(k, 4){
					int newx = i + 1 + dx[k];
					int newy = j + 1 + dy[k];
					if (valid(newx, newy) && grid[newx][newy] == '.'){
						adj[W*i + (j+1)][W*(newx-1)+ newy] = 1;
					}
				}
			}
		}
	}
	FOR(i, 1, W*H+1){
		FOR(j, 1, W*H+1){
			if (i == j) dist[i][j] = 0;
			else if (adj[i][j] != 0) dist[i][j] = 1;
			else dist[i][j] = inf;
		}
	}
	int max_dist = 0;
	FOR(i,1,W*H+1){
		FOR(j,1,W*H+1){
			FOR(k, 1,W*H+1){
				dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
			}
		}
	}
	F0R(i,H){
		F0R(j,W){
			F0R(k,H){
				F0R(l,W){
					if (grid[i+1][j+1] == '.' && grid[k+1][l+1] == '.'){
						max_dist = max(max_dist, dist[W*i + j+1][W*k + l+1]);


					}
				}
			}
		}
	}
	cout << max_dist << newl;



























	return 0;
}