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>

using namespace std;

typedef long long ll;
const ll INF = 1LL<<60;

ll gcd(ll a, ll b) {
  if(b == 0) return a;
  return gcd(b, a % b);
}

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int maze[40][40] = {0};
int d1[40][40];
int d2[40][40];

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  
  string S;
  int start_i;
  int start_j;

  for(int i = 0; i < 40; i++){
    for(int j = 0; j < 40; j++){
      maze[i][j] = 0;
    }
  }

  for(int i = 1; i <= H; i++){
    cin >> S;
    for(int j = 1; j <= W; j++){
      if(S[j-1] == '.'){
        maze[i][j] = 1;
        start_i = i;
        start_j = j;
      }else{
        maze[i][j] = 0;
      }
    }
  }

  for(int i = 0; i < 40; i++){
    for(int j = 0; j < 40; j++){
      d1[i][j] = -1;
      d2[i][j] = -1;
    }
  }

  d1[start_i][start_j] = 0;

  queue<pair<int, int>> q;

  q.push(make_pair(start_i, start_j));

  while(!q.empty()){
    auto next = q.front();
    int ni = next.first;
    int nj = next.second;

    q.pop();

    if(maze[ni+1][nj] == 1){

      if(d1[ni+1][nj] == -1){
        d1[ni+1][nj] = d1[ni][nj] + 1;
        q.push(make_pair(ni+1, nj));
      }
    }

    if(maze[ni-1][nj] == 1){

      if(d1[ni-1][nj] == -1){
        d1[ni-1][nj] = d1[ni][nj] + 1;
        q.push(make_pair(ni-1, nj));
      }
    }

    if(maze[ni][nj+1] == 1){

      if(d1[ni][nj+1] == -1){
        d1[ni][nj+1] = d1[ni][nj] + 1;
        q.push(make_pair(ni, nj+1));
      }
      
    }

    if(maze[ni][nj-1] == 1){

      if(d1[ni][nj-1] == -1){
        d1[ni][nj-1] = d1[ni][nj] + 1;
        q.push(make_pair(ni, nj-1));
      } 
    }
  }

  int starti2;
  int startj2;
  int dmax = -1;

  for(int i = 1; i <= H; i++){
    for(int j = 1; j <= W; j++){
      if(chmax(dmax, d1[i][j])){
        starti2 = i;
        startj2 = j;
      }
    }
  }

  d2[starti2][startj2] = 0;
  q.push(make_pair(starti2, startj2));

  while(!q.empty()){
    auto next = q.front();
    int ni = next.first;
    int nj = next.second;

    q.pop();

    if(maze[ni+1][nj] == 1){

      if(d2[ni+1][nj] == -1){
        d2[ni+1][nj] = d2[ni][nj] + 1;
        q.push(make_pair(ni+1, nj));
      }

    }

    if(maze[ni-1][nj] == 1){

      if(d2[ni-1][nj] == -1){
        d2[ni-1][nj] = d2[ni][nj] + 1;
        q.push(make_pair(ni-1, nj));
      }
      
    }

    if(maze[ni][nj+1] == 1){

      if(d2[ni][nj+1] == -1){
        d2[ni][nj+1] = d2[ni][nj] + 1;
        q.push(make_pair(ni, nj+1));
      }
      
    }

    if(maze[ni][nj-1] == 1){

      if(d2[ni][nj-1] == -1){
        d2[ni][nj-1] = d2[ni][nj] + 1;
        q.push(make_pair(ni, nj-1));
      } 
    }
  }

  int ans = -1;
  for(int i = 1; i <= H; i++){
    for(int j = 1; j <= W; j++){
      chmax(ans, d2[i][j]);
    }
  }

  cout << ans << endl;  

  return 0;
}