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 REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { std::cout << #a << " = " << a << "\n"; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }
template<class T, class V> istream& operator>> (istream& ist, pair<T, V>& p) { return ist >> p.first >> p.second; }
template<class T> ostream& operator<< (ostream& ost, pair<T, T>& p) { return ost << p.first << ", " << p.second; }
template<class T> istream& operator>> (istream& ist, vector<T>& vs) { for(auto& e: vs) ist >> e; return ist; }

typedef long long ll;
int const inf = INT_MAX / 2;

const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

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

int get_max_distance(vector<string> const& F, int H, int W, int si, int sj) {
  queue<pair<int, int>> q;
  q.emplace(si, sj);
  vector<vector<int>> dist(H, vector<int>(W, inf));
  dist[si][sj] = 0;
  int ans = 0;
  while (!q.empty()) {
    const auto p = q.front(); q.pop();
    const auto y = p.first;
    const auto x = p.second;
    rep(k, 4) {
      const auto ny = y + dy[k];
      const auto nx = x + dx[k];
      if (!in_field(ny, nx, H, W)) { continue; }
      if (F[ny][nx] == '#') { continue; }
      if (dist[ny][nx] < inf) { continue; }
      dist[ny][nx] = dist[y][x] + 1;
      ans = max(ans, dist[ny][nx]);
      q.emplace(ny, nx);
    }
  }
  return ans;
}

int main() {
  int H, W; cin >> H >> W;
  vector<string> F(H);
  rep(i, H) cin >> F[i];
  int ans = 0;
  rep(i, H) rep(j, W) {
    if (F[i][j] == '#') { continue; }
    ans = max(ans, get_max_distance(F, H, W, i, j));
  }
  cout << ans << endl;
}