#include #include #include #include using namespace std; class Cum2 { public: vector> cum; Cum2(const vector>& A) { int H = A.size(); int W = A[0].size(); cum = vector>(H + 1, vector(W + 1, 0)); for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cum[i + 1][j + 1] = A[i][j]; } } for (int i = 1; i <= H; ++i) { for (int j = 2; j <= W; ++j) { cum[i][j] += cum[i][j - 1]; } } for (int i = 2; i <= H; ++i) { for (int j = 1; j <= W; ++j) { cum[i][j] += cum[i - 1][j]; } } } int query(int lh, int lw, int rh, int rw) { return cum[rh + 1][rw + 1] + cum[lh][lw] - cum[rh + 1][lw] - cum[lh][rw + 1]; } }; int H, W; vector S; bool func(int n, Cum2& cum) { vector> imos(H + 1, vector(W + 1, 0)); for (int i = 0; i <= H - n; ++i) { for (int j = 0; j <= W - n; ++j) { if (cum.query(i, j, i + n - 1, j + n - 1) == n * n) { imos[i][j] += 1; imos[i + n][j + n] += 1; imos[i][j + n] -= 1; imos[i + n][j] -= 1; } } } for (int i = 0; i <= H; ++i) { for (int j = 1; j <= W; ++j) { imos[i][j] += imos[i][j - 1]; } } for (int i = 1; i <= H; ++i) { for (int j = 0; j <= W; ++j) { imos[i][j] += imos[i - 1][j]; } } for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (imos[i][j] >= 1) { if (S[i][j] != '#') return false; } else { if (S[i][j] != '.') return false; } } } return true; } int main() { cin >> H >> W; S.resize(H); for (int i = 0; i < H; ++i) { cin >> S[i]; } vector> C(H, vector(W, 0)); for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (S[i][j] == '#') { C[i][j] = 1; } } } Cum2 cum(C); int left = 1, right = min(H, W) + 1; while (left + 1 < right) { int mid = (left + right) / 2; if (func(mid, cum)) { left = mid; } else { right = mid; } } cout << left << endl; return 0; }