H,W=map(int,input().split()) S=[input() for i in range(H)] v=[[0]*W for i in range(H)] for i in range(H): for j in range(W): if S[i][j]=='#': v[i][j]+=1 t=0 x1,x2=i+1,i-1 if x1>=H: t+=1 else: if S[x1][j]=='.': t+=1 if x2<0: t+=1 else: if S[x2][j]=='.': t+=1 if t==2: print(1) exit() t=0 y1,y2=j+1,j-1 if y1>=W: t+=1 else: if S[i][y1]=='.': t+=1 if y2<0: t+=1 else: if S[i][y2]=='.': t+=1 if t==2: print(1) exit() for i in range(H): for j in range(1,W): v[i][j]=v[i][j-1]+v[i][j] for i in range(1,H): for j in range(W): v[i][j]=v[i-1][j]+v[i][j] result=10**10 DX=[1,1,-1,-1] DY=[1,-1,1,-1] for i in range(H): for j in range(W): if S[i][j]=='.': continue for dx,dy in zip(DX,DY): x=i-dx y=j-dy t=0 if x<0 or x>=H: t+=1 else: if S[x][j]=='.': t+=1 if y<0 or y>=W: t+=1 else: if S[i][y]=='.': t+=1 if t==2: l=1 r=2000 while True: m=(l+r+1)//2 i2,j2=i+dx*(m-1),j+dy*(m-1) if i2<0 or i2>=H or j2<0 or j2>=W: r=m-1 else: j3=max(j,j2) j4=min(j,j2) i3=max(i,i2) i4=min(i,i2) ans=v[i3][j3] if j4>0: ans-=v[i3][j4-1] if i4>0: ans-=v[i4-1][j3] if i4>0 and j4>0: ans+=v[i4-1][j4-1] if ans==m**2: l=m else: r=m-1 if l==r: break result=min(result,l) print(result)