M=410
e=enumerate
_,*s=open(0)
r=s+['#'*20]
d=eval('[M]*M,'*M)
d[0][0]=0
for i,t in e(s):
  for j,u in e(t):
    k=i*20+j
    if u>'#'<t[j+1]:
      d[k][k+1]=1
      d[k+1][k]=1
    if u>'#'<r[i+1][j]:
      d[k][k+20]=1
      d[k+20][k]=1
r=range(M)
for k in r:
  for i in r:
    for j in r:d[i][j]=min(d[i][j],d[i][k]+d[k][j])
print(max(u for t in d for u in t if u<M))