/*
Author: Racer5x
 ***************************************   UNAUTHORISED COPYING OF CODE PROHIBITED   **********************************
*/
 
/*#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
 
#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define pb          emplace_back
#define pf          emplace_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        998244353 
#define PI          3.141592653589
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MAX         2000000000000000000
#define M           1000000007
 
using namespace std;
 
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int power(int x,int n){
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x);
        x=(x*x);
        n=n/2;
    }
    return result;
}
 
class dsu{
    vi p,rank;
    vi set_count;
  
  public:
    dsu(int n){
        p.assign(n,0); 
        rank.assign(n,0);
        set_count.assign(n,1);
        for(int i=0;i<n;i++) p[i] = i;
    }
    int findSet(int x){
        return (p[x] == x) ? x:(p[x] = findSet(p[x]));
    }
    bool isSame(int i, int j){
        return (findSet(i) == findSet(j));
    }
    void unionSet(int i, int j){
        if(!isSame(i,j)){
            int x = findSet(i), y = findSet(j);
            if(rank[x] > rank[y]) {p[y] = x; set_count[x]+=set_count[y];}
            else{
                p[x] = y;
                set_count[y]+=set_count[x];
                if(rank[x] == rank[y]) rank[y]++;
            }
        }
    }
    int count(int x){
        return set_count[findSet(x)];
    }
};

char mat[25][25];
int h, w;
bool isIn(int x, int y){
    return (x>=0 && y>=0 && x<h && y<w && mat[x][y] == '.');
}
 
signed main()
{
    tezz
 
    cin>>h>>w;

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++) cin>>mat[i][j];
    }

    queue<pair<int, pii>> q;

    int vx[] = {0, 0, -1, 1};
    int vy[] = {1, -1, 0, 0};
    int mx = 0;

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(mat[i][j] == '#') continue;
            int xx = i, yy = j;
            bool vis[25][25] = {false};

            for(int k=0;k<h;k++){
                for(int l=0;l<w;l++){
                    if(mat[k][l] == '#') continue;
                    int xd = k, yd = l;
                    memset(vis, false, sizeof(vis));

                    q.push({0, {xx, yy}});
                    vis[xx][yy] = true;

                    while(!q.empty()){
                        pair<int, pii> p = q.front();
                        q.pop();
                        int dx = p.y.x, dy = p.y.y;
                        int lvl = p.x;

                        bool flag = false;
                        for(int t=0;t<4;t++){
                            if(isIn(dx + vx[t], dy + vy[t]) && !vis[dx + vx[t]][dy + vy[t]]){
                                if(dx + vx[t] == xd && dy + vy[t] == yd){
                                    if(mx < lvl + 1){
                                        mx = lvl + 1;
                                    }
                                    // cout<<xx<<' '<<yy<<" -> "<<xd<<' '<<yd<<" = "<<lvl+1<<endl;
                                    flag = true;
                                    break;
                                 }
                                vis[dx + vx[t]][dy + vy[t]] = true;
                                q.push({lvl+1, {dx + vx[t], dy + vy[t]}});
                            }
                        }
                        if(flag){
                            while(!q.empty()) q.pop();
                            break;
                        }
                    }
                }
            }
        }
    }

    cout<<mx;
}

Measured memory_bytes: 256