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: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define sll(x) scanf("%lld",&x)
#define prll(x) printf("%lld ",x)
#define pri(x) printf("%d ",x)
#define si(x) scanf("%d",&x)
#define el cout << "\n"
#define pb push_back
#define f first
#define s second
#define arrondi(a) (long long)floor(a+0.5);
#define NIL -1
ll N=500005;
ll MAX=9223372036854775807;
ll MOD=1000000007;
long double PI =4*atan(1);
ll n,m;
vector <string> A;
int dx[] = {1,0,0,-1};
int dy[] = {0,1,-1,0};
ll dist[30][30];
bool valid(int x,int y)
{
    return (x>=1 && x<=n && y>=1 && y<=m);
}
bool visited[30][30];
ll ans;
void bfs(pair<ll,ll> source)
{
    queue <pair<ll,ll> >q;
    q.push(source);
    dist[source.first][source.second]=0;
    visited[source.first][source.second]=true;
    while(!q.empty())
    {
        pair<ll,ll> v=q.front();
        q.pop();
        for (int i=0;i<4;i++)
        {
            ll row=v.first+dx[i];
            ll col=v.second+dy[i];
            if (valid(row,col) && A[row-1][col-1]=='.' && visited[row][col]==false)
            {
                visited[row][col]=true;
                dist[row][col]=dist[v.first][v.second]+1;
                ans=max(dist[row][col],ans);
                q.push(make_pair(row,col));
            }
        }
    }
}
int main()
{
    IOS;
    cout.precision(30);
    string s;
    cin >> n >> m;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){visited[i][j]=false; dist[i][j]=MAX;}

    for(int i = 1; i <= n; i++)
    {
        cin >> s;
        A.push_back(s);
    }
    ll ANS=0;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            if (A[i-1][j-1]=='.')
            {
                ans=0;
                bfs(make_pair(i,j));
                memset(visited,false,sizeof visited);
                ANS=max(ANS,ans);
            }
        }
    }
    cout << ANS << "\n";
}