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

#define rep(i,n)  for(int i=0;i<n;++i)
#define per(i,n)  for(int i=n-1;i>=0;--i)
#define sc1(a)  scanf("%d",&a)
#define sc2(a,b)  scanf("%d %d",&a,&b)
#define sc3(a,b,c)  scanf("%d %d %d",&a,&b,&c)
#define sl1(a)  scanf("%lld",&a)
#define sl2(a,b)  scanf("%lld %lld",&a,&b)
#define sl3(a,b,c)  scanf("%lld %lld %lld",&a,&b,&c)
#define PI 3.1415926535897932

int main(){
    int mod=1000000007;
    int h,w,ans=1;
    sc2(h,w);
    int d[h][w];
    rep(i,h) rep(j,w) d[i][j]=0; // (0,".")  (1,"#")
    rep(i,h) rep(j,w) {
        char s[w];
        scanf("%s",s);
        rep(k,w) if (s[k]=='#') d[i][k]=1;
    }
    int p[h*w][h*w];
    rep(i,h*w) rep(j,w*h) p[i][j]=1000; 

    rep(i,h*w) rep(j,h*w) rep(k,h*w) {
        int ih=i/w,iw=i%w, jh=j/w,jw=j%w, kh=k/w,kw=k%w;
            if ("."==d[ih][iw]==d[jh][jw] and ih==jh and abs(iw-jw)==1) p[i][j]=1; p[j][i]=1;
            if ("."==d[ih][iw]==d[jh][jw] and iw==jw and abs(ih-jh)==1) p[i][j]=1; p[j][i]=1;
            if ("."==d[ih][iw]==d[kh][kw] and ih==kh and abs(iw-kw)==1) p[i][k]=1; p[k][i]=1;
            if ("."==d[ih][iw]==d[kh][kw] and iw==kw and abs(ih-kh)==1) p[i][k]=1; p[k][i]=1;
            if ("."==d[jh][jw]==d[kh][kw] and jh==kh and abs(jw-kw)==1) p[j][k]=1; p[k][j]=1;
            if ("."==d[jh][jw]==d[kh][kw] and jw==kw and abs(jh-kh)==1) p[j][k]=1; p[k][j]=1;
            p[j][k]=min(p[j][k],p[j][i]+p[i][k]);
    }
    rep(i,h*w) rep(j,w*h) if (p[i][h]!=1000) ans=max(ans,p[i][h]);

    printf("%d\n",ans);
    return 0;
}
