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: Wrong Answer)

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define rrep(i, a, n) for (int i = (int)(a); i >= (int)(n); --i)
#define debug(x) cerr << #x << " = " << x << "\n"
#define debugv(x) \
  rep(f, 0, (x.size() - 1)) cerr << x[f] << (f == (x.size() - 1) ? "\n" : " ")
#define debug2(x, y)                       \
  cerr << "(" << #x << "," << #y << ") = " \
       << "(" << x << "," << y << ")\n"
#define all(x) x.begin(), x.end()
#define each(a, x) for (auto& a : (x))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define int long long
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
const int MX = 1e5 + 5, INF = 5LL << 57, MOD = 1e9 + 7;

int H, W;
string S;
vi va, vb;
int field[24][24];

void input() {
  cin >> H >> W;
  rep(i, 1, H) {
    ;
    ;
    rep(j, 1, W) {
      char c;
      cin >> c;
      field[i][j] = (c == '#' ? 0 : 1);
      ;
    }
  };
  ;
}
int dh[4] = {-1, 0, 0, 1};
int dw[4] = {0, -1, 1, 0};
int dp[24][24][24][24];

void solve() {
  rep(i, 1, H) {
    rep(j, 1, W) {
      rep(k, 1, H) {
        rep(l, 1, H) { dp[i][j][k][l] = -1; }
      }
    }
  }

  rep(h, 1, H) {
    rep(w, 1, W) {
      // dp[h][w][h + 1][w] = (field[h + 1][w] ? dp[h][w][h][w] + 1 : -1);
      // dp[h][w][h][w + 1] = (field[h][w + 1] ? dp[h][w][h][w] + 1 : -1);

      queue<pii> q;
      if (field[h][w] == 0) {
        continue;
      } else {
        dp[h][w][h][w] = 0;
        q.push(pii(h, w));
      }
      while (q.size()) {
        pii qq = q.front();
        q.pop();
        int nh = qq.first, nw = qq.second;
        rep(i, 0, 3) {
          int ph = nh + dh[i];
          int pw = nw + dw[i];
          if (field[ph][pw] == 0) {
            dp[h][w][ph][pw] = -1;
            continue;
          }
          if (dp[h][w][ph][pw] >= 0) {
            continue;
          }

          dp[h][w][ph][pw] = dp[h][w][nh][nw] + 1;
          q.push(pii(ph, pw));
          // debug2(h, w);
          //   debug2(ph, pw);
        }
      }
    }
  }
  int ans = -1;
  rep(i, 1, H) {
    rep(j, 1, W) {
      rep(k, 1, H) {
        rep(l, 1, W) { chmax(ans, dp[i][j][k][l]); }
      }
    }
  }
  cout << max(ans, 0LL) << endl;
}
signed main() {
  input();
  solve();
  return 0;
}