/* -*- coding: utf-8 -*- * * 2456.cc: No.2456 Stamp Art - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_H = 2000; const int MAX_W = 2000; /* typedef */ template struct BIT { int n; vector bits; BIT() {} BIT(int _n) { init(_n); } void init(int _n) { n = _n; bits.assign(n + 1, 0); } T sum(int x) { x = min(x, n); T s = 0; while (x > 0) { s += bits[x]; x -= (x & -x); } return s; } void add(int x, T v) { if (x <= 0) return; while (x <= n) { bits[x] += v; x += (x & -x); } } }; template struct BIT2D { int n; vector> bits; BIT2D() {} BIT2D(int _ny, int _nx) { init(_ny, _nx); } void init(int _ny, int _nx) { n = _ny; bits.assign(n + 1, BIT()); for (int y = 1; y <= n; y++) bits[y].init(_nx); } T sum(int y, int x) { y = min(y, n); T s = 0; while (y > 0) { s += bits[y].sum(x); y -= (y & -y); } return s; } void add(int y, int x, T v) { if (y <= 0) return; while (y <= n) { bits[y].add(x, v); y += (y & -y); } } }; /* global variables */ char fs[MAX_H][MAX_W + 4]; int ass[MAX_H + 1][MAX_W + 1]; /* subroutines */ inline int rect(int y0, int x0, int y1, int x1) { return ass[y1][x1] - ass[y1][x0] - ass[y0][x1] + ass[y0][x0]; } bool check(int h, int w, int k) { BIT2D bit; bit.init(h, w); int kk = k * k; for (int y = 0; y + k <= h; y++) for (int x = 0; x + k <= w; x++) if (rect(y, x, y + k, x + k) == kk) { bit.add(y + 1, x + 1, 1); bit.add(y + k + 1, x + 1, -1); bit.add(y + 1, x + k + 1, -1); bit.add(y + k + 1, x + k + 1, 1); } for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { int c = bit.sum(y + 1, x + 1); if ((fs[y][x] == '#' && c == 0) || (fs[y][x] == '.' && c > 0)) return false; } return true; } /* main */ int main() { int h, w; scanf("%d%d", &h, &w); for (int y = 0; y < h; y++) scanf("%s", fs[y]); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) ass[y + 1][x + 1] = (fs[y][x] == '#' ? 1 : 0) + ass[y + 1][x] + ass[y][x + 1] - ass[y][x]; int k0 = 1, k1 = min(h, w) + 1; while (k0 + 1 < k1) { int k = (k0 + k1) / 2; if (check(h, w, k)) k0 = k; else k1 = k; } printf("%d\n", k0); return 0; }