import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(1000005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = -1-(-1 << 63) # md = 10**9+7 md = 998244353 class CumSum2D: def __init__(self, arr, w=None): if w: self.h = arr self.w = w else: self.h = len(arr) self.w = len(arr[0]) self.t = [0]*(self.h+1)*(self.w+1) if w == None: w = self.w+1 for i, row in enumerate(arr): self.t[(i+1)*w+1:(i+1)*w+w] = row self._finalized = False def add(self, i, j, c): w = self.w+1 self.t[(i+1)*w+j+1] += c def finalize(self): w = self.w+1 self._finalized = True for ij in range(w+1, (self.h+1)*w): i, j = divmod(ij, w) if j == 0: continue if j+1 < w: self.t[i*w+j+1] += self.t[i*w+j] self.t[i*w+j] += self.t[(i-1)*w+j] def sum(self, ij0, ij1=None): if not self._finalized: print("Finalize!!!") exit() w = self.w+1 i, j = ij0 x, y = self.h, self.w if ij1: x, y = ij1 if i >= x or j >= y: return 0 return self.t[i*w+j]+self.t[x*w+y]-self.t[i*w+y]-self.t[x*w+j] def imos_add(t, w, i0, j0, i1, j1, c): t[i0*(w+1)+j0] += c t[i1*(w+1)+j1] += c t[i0*(w+1)+j1] -= c t[i1*(w+1)+j0] -= c def imos_finalize(t, w): w += 1 for ij in range(len(t)-w): i, j = divmod(ij, w) if j == w-1: continue t[i*w+j+1] += t[i*w+j] if i: t[i*w+j] += t[(i-1)*w+j] def binary_search(l, r, minimize): if minimize: l -= 1 else: r += 1 while l+1 < r: m = (l+r)//2 if ok(m) ^ minimize: l = m else: r = m if minimize: return r return l def ok(m): m2 = m**2 cur = [0]*(h+1)*(w+1) for i in range(h-m+1): for j in range(w-m+1): if cs.sum([i, j], [i+m, j+m]) == m2: imos_add(cur,w,i,j,i+m,j+m,1) imos_finalize(cur,w) for i in range(h): for j in range(w): if (cur[i*(w+1)+j] > 0) != (ss[i][j] > 0): return False return True h, w = LI() ss = [[(c == "#")*1 for c in SI()] for _ in range(h)] cs = CumSum2D(ss) cs.finalize() tt = [] for s in ss: tt += s+[0] print(binary_search(1, min(h, w), False))