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 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <assert.h>

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <set>
#include <utility>

using namespace std;

int main(int argc, char** argv) {
  int h, w;
  cin >> h >> w;
  
  using coord = pair<int, int>;
  queue<coord> starts;
  char maze[h+1][w+1];
  int dp[h+1][w+1];

  for (int i = 0; i < w + 1; ++i) {
    maze[0][i] = '#';
    maze[h][i] = '#';
  }
  for (int i = 0; i < h + 1; ++i) {
    maze[i][0] = '#';
    maze[i][w] = '#';
  }
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < s.size(); ++j) {
      maze[i + 1][j + 1] = s[j];
    }
  }
  
  for (int i = 0; i < h + 1; ++i) {
    for (int j = 0; j < w + 1; ++j) {
      if (maze[i][j] != '#')
        starts.push(coord(j,i));
    }
  }

  int dist = 0;
  while (!starts.empty()) {
    coord cur = starts.front();
    starts.pop();

    for (int i = 0; i < h + 1; ++i) {
      for (int j = 0; j < w + 1; ++j) {
        if (maze[i][j] == '#')
          dp[i][j] = -1;
        else
          dp[i][j] = 0;
      }
    }

    queue<coord> next;
    next.push(cur);
    while (!next.empty()) {
      cur = next.front();
      next.pop();

      int x = cur.first;
      int y = cur.second;
      // cout << x << "," << y << "(" << dp[y][x] << "):";

      int minp = h*w;
      // look around to find non-zero min
      // if zero add to next queue
      switch (dp[y-1][x]) {
        case 0: next.push(coord(x,y-1));
        case -1: break;
        default:
          minp = min(minp, dp[y-1][x]);
      }
      switch (dp[y+1][x]) {
        case 0: next.push(coord(x,y+1));
        case -1: break;
        default:
          minp = min(minp, dp[y+1][x]);
      }
      switch (dp[y][x-1]) {
        case 0: next.push(coord(x-1,y));
        case -1: break;
        default:
          minp = min(minp, dp[y][x-1]);
      }
      switch (dp[y][x+1]) {
        case 0: next.push(coord(x+1,y));
        case -1: break;
        default:
          minp = min(minp, dp[y][x+1]);
      }

      // fill min + 1
      if (minp < h * w) 
        dp[y][x] = minp + 1;
      else
        dp[y][x] = 1;

      dist = max(dist, dp[y][x]);
      // cout << dp[y][x] << " size=" << next.size() << endl;;
    }
  }
  cout << dist - 1 << endl;
}