import sys input = sys.stdin.readline H, W = map(int, input().split()) S = [input().rstrip() for _ in range(H)] cumsum = [[0] * (W+1) for _ in range(H+1)] for i in range(H): for j in range(W): cumsum[i+1][j+1] = cumsum[i][j+1] + cumsum[i+1][j] - \ cumsum[i][j] + (1 if S[i][j] == '#' else 0) lb, ub = 0, 2000 while ub-lb > 1: m = (lb+ub)//2 painted = [[0]*(W+1) for _ in range(H+1)] for i in range(H-m+1): for j in range(W-m+1): if cumsum[i+m][j+m]-cumsum[i+m][j]-cumsum[i][j+m]+cumsum[i][j] == m*m: painted[i][j] += 1 painted[i+m][j] -= 1 painted[i][j+m] -= 1 painted[i+m][j+m] += 1 for i in range(H+1): for j in range(W): painted[i][j+1] += painted[i][j] for j in range(W+1): for i in range(H): painted[i+1][j] += painted[i][j] ok = True for i in range(H): for j in range(W): if S[i][j] == '#' and not painted[i][j]: ok = False break if not ok: break if ok: lb = m else: ub = m print(lb)