#include using namespace std; using ll = long long; template istream& operator >> (istream& is, vector& vec) { for(T& x : vec) is >> x; return is; } template ostream& operator << (ostream& os, const vector& vec) { if(vec.empty()) return os; os << vec[0]; for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it; return os; } template struct CumulativeSum2D{ int h, w; std::vector> dat; CumulativeSum2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector(W + 1, 0)) {} CumulativeSum2D(std::vector> &A) : h(A.size()), w(A[0].size()), dat(h + 1, std::vector(w + 1, 0)) { for(int y = 1; y <= h; y++){ for(int x = 1; x <= w; x++){ dat[y][x] = A[y - 1][x - 1] + dat[y][x - 1] + dat[y - 1][x] - dat[y - 1][x - 1]; } } } void add(int y, int x, T z){ assert(0 <= y && y < h); assert(0 <= x && x < w); dat[y + 1][x + 1] += z; } void build(){ for(int y = 1; y <= h; y++) { for(int x = 1; x <= w; x++) { dat[y][x] += dat[y][x - 1] + dat[y - 1][x] - dat[y - 1][x - 1]; } } } T query(int ly, int lx, int ry, int rx){ assert(0 <= ly && ly <= ry && ry <= h); assert(0 <= lx && lx <= rx && rx <= w); return dat[ry][rx] - dat[ly][rx] - dat[ry][lx] + dat[ly][lx]; } }; template struct imos2D{ int h, w; std::vector> dat; imos2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector(W + 1, 0)) {} void add(int ly, int lx, int ry, int rx, T v){ assert(0 <= ly && ly <= ry && ry <= h); assert(0 <= lx && lx <= rx && rx <= w); dat[ry][rx] += v; dat[ly][rx] -= v; dat[ry][lx] -= v; dat[ly][lx] += v; } void build(){ for(int i = 0; i <= h; i++) { for(int j = 1; j <= w; j++) { dat[i][j] += dat[i][j - 1]; } } for(int i = 0; i <= w; i++) { for(int j = 1; j <= h; j++) { dat[j][i] += dat[j - 1][i]; } } } const std::vector& operator[](int y) const { assert(0 <= y && y < h); return dat[y]; } std::vector& operator[](int y) { assert(0 <= y && y < h); return dat[y]; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; vector A(h); cin >> A; CumulativeSum2D S(h, w); for(int y = 0; y < h; y++){ for(int x = 0; x < w; x++){ if(A[y][x] == '#') S.add(y, x, 1); } } auto f = [&](int L){ imos2D S2(h, w); for(int y = 0; y + L <= h; y++){ for(int x = 0; x + L <= w; x++){ if(S.query(y, x, y + L, x + L) == L * L){ S2.add(y, x, y + L, x + L, 1); } } } S2.build(); for(int y = 0; y < h; y++){ for(int x = 0; x < w; x++){ if(A[y][x] == '#' && S2[y][x] == 0) return false; } } return true; }; S.build(); int ok = 1, ng = min(h, w) + 1, mid; while(ok + 1 < ng){ mid = (ok + ng) / 2; if(f(mid)) ok = mid; else ng = mid; } cout << ok << '\n'; }