from scipy.sparse import*
e=enumerate
h,*s=open(0)
h,w=map(int,h.split())
*g,=eval('[0]*500,'*500)
for i,t in e(s):
  for j,u in e(t):
    k=i*w+j
    g[k][k+1]=u>'#'<t[j+1]
    g[k][k+w]=(i<h-1)*u>'#'<s[i+1][j]  
a=csgraph.johnson(csr_matrix(g),0)
print(a[a<999].max())