//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//Abdulhamid Han
//Bismillahirrahmanirrahim
//█▀█─█──█──█▀█─█─█
//█▄█─█──█──█▄█─█▄█
//█─█─█▄─█▄─█─█─█─█

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;
typedef pair< lo,PII > PIII;

#define fi first
#define se second
#define mp make_pair
#define int long long
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo MAX = -1000000000000000000;
const lo MIN = 1000000000000000000;
const lo inf = 1000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,mx,vis[25][25];
int cev;
char s[25][25];
vector<int> v;

inline void bfs(int bas,int basy){
	queue<PIII> q;
	//~ cout<<bas<<" "<<basy<<endl;
	while(q.size())q.pop();
	q.push(mp(bas,mp(basy,0)));
	while(q.size()){
		int satir=q.front().fi;
		int sutun=q.front().se.fi;
		int der=q.front().se.se;
		q.pop();
		if(satir<1 || sutun<1 || satir>n || sutun>m)continue;
		if(s[satir][sutun]=='#')continue;
		if(vis[satir][sutun]==1)continue;
		vis[satir][sutun]=1;
		mx=max(mx,der);
		q.push(mp(satir+1,mp(sutun,der+1)));
		q.push(mp(satir-1,mp(sutun,der+1)));
		q.push(mp(satir,mp(sutun+1,der+1)));
		q.push(mp(satir,mp(sutun-1,der+1)));
	}
}

main(void){
	scanf("%lld %lld",&n,&m);
	FOR{
		scanf("%s",(s[i]+1));
	}
	bfs(1,1);
	memset(vis,0,sizeof(vis));
	bfs(1,m);
	memset(vis,0,sizeof(vis));
	bfs(n,1);
	memset(vis,0,sizeof(vis));
	bfs(n,m);
	printf("%lld\n",mx);
	return 0;
}

