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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<long long , long long>
#define pb push_back
#define ll long long
#define ld long double
#define precision(x,d) cout<<fixed<<setprecision(d)<<x
#define fill(a,b) memset((a),(b),sizeof((a)))
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
ll power(ll x,ll y,ll m){ll r=1;for(;y;y>>=1){if(y&1)r=r*x%m;x=x*x%m;}return r;}
const ll mod = 1e9 + 7;
const ld pi = 3.14159265358979;
    
int arr[21][21], visited[21][21], level[21][21], h, w;

int r[4] = {0, 1, -1, 0};
int c[4] = {1, 0, 0, -1};

int check(int i, int j){
    if(i >= 0 && j >= 0 && i < h && j < w) return 1;
    return 0;
}

int dfs(int x, int y){
    queue<int> q;
    int k = 0;
    q.push(x); q.push(y);
    while(!q.empty()){
        int a = q.front(); q.pop();
        int b  = q.front(); q.pop();
        visited[a][b] = 1;
        for(int i=0; i<4; i++){
            if(arr[a + r[i]][b + c[i]] == 1 && check(a + r[i], b + c[i]) && !visited[a + r[i]][b + c[i]]){
                q.push(a + r[i]); q.push(b + c[i]);
                level[a + r[i]][b + c[i]] = level[a][b] + 1;
                k = max(k, level[a + r[i]][b + c[i]]);
            }
        }
    }
    return k;
}

int run_test(){
    cin >> h >> w;
    for(int i=0; i<h; i++){
        for(int j=0; j < w; j++){
            char t;
            cin >> t;
            if(t == '#') arr[i][j] = 0;
            else{
                arr[i][j] = 1;
            }
        }
    } 
    for(int i=0; i< h; i++){
        for(int j=0; j< w; j++){
            level[i][j] = visited[i][j] = 0;
        }
    }
    int k, l, m, ans = 0;
    for(int i=0; i< h; i++){
        for(int j=0; j< w; j++){
            if(arr[i][j] == 1 && !visited[i][j]){
                k = dfs(i, j);
                for( l = 0; l < h; l++){
                    for( m=0; m < w; m++){
                        if(level[l][m] == k){
                            break;
                        }
                    }
                }
                for(int p =0; p < h; p++){
                    for(int q=0; q< w; q++){
                        level[p][q] = visited[p][q] = 0;
                    }
                }
                k = dfs(l ,m);
                ans = max(ans, k);
            }
        }
    }
    cout << ans - 1;
    return 0;
}
    
int main(){
   FAST;
    ll t;
    t = 1;
     
    while(t--){
        run_test();
    }
}