#include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct CumulativeSum2D { explicit CumulativeSum2D(const int h, const int w) : CumulativeSum2D(std::vector>(h, std::vector(w, 0))) {} template explicit CumulativeSum2D(const std::vector>& a) : is_built(false), h(a.size()), w(a.front().size()), data(h + 1, std::vector(w + 1, 0)) { for (int i = 0; i < h; ++i) { std::copy(a[i].begin(), a[i].end(), std::next(data[i + 1].begin())); } } void add(const int y, const int x, const T val) { assert(!is_built); data[y + 1][x + 1] += val; } void build() { assert(!is_built); is_built = true; for (int i = 0; i < h; ++i) { std::partial_sum(data[i + 1].begin(), data[i + 1].end(), data[i + 1].begin()); } for (int j = 1; j <= w; ++j) { for (int i = 1; i < h; ++i) { data[i + 1][j] += data[i][j]; } } } T query(const int y1, const int x1, const int y2, const int x2) const { assert(is_built); return y1 > y2 || x1 > x2 ? 0 : data[y2 + 1][x2 + 1] - data[y2 + 1][x1] - data[y1][x2 + 1] + data[y1][x1]; } private: bool is_built; const int h, w; std::vector> data; }; int main() { int h, w; cin >> h >> w; vector s(h); REP(i, h) cin >> s[i]; CumulativeSum2D cs(h, w); REP(i, h) REP(j, w) { if (s[i][j] == '#') cs.add(i, j, 1); } cs.build(); int lb = 1, ub = min(h, w) + 1; while (ub - lb > 1) { const int k = midpoint(lb, ub); vector grid(h, vector(w, 0)); REP(i, h - k + 1) REP(j, w - k + 1) { if (cs.query(i, j, i + k - 1, j + k - 1) == k * k) { ++grid[i][j]; if (i + k < h) --grid[i + k][j]; if (j + k < w) --grid[i][j + k]; if (i + k < h && j + k < w) ++grid[i + k][j + k]; } } REP(i, h) FOR(j, 1, w) grid[i][j] += grid[i][j - 1]; REP(j, w) FOR(i, 1, h) grid[i][j] += grid[i - 1][j]; bool is_valid = true; REP(i, h) REP(j, w) { if (s[i][j] == '#' && grid[i][j] == 0) { is_valid = false; break; } } (is_valid ? lb : ub) = k; } cout << lb << '\n'; return 0; }