#include #include #define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) using namespace std; using namespace atcoder; typedef long long ll; const int inf = 1e9; struct RMUQ { using S = ll; using F = ll; public: RMUQ() : RMUQ(0) {} RMUQ(int n) : RMUQ(std::vector(n, e())) {} RMUQ(const std::vector& v) : _n(int(v.size())) { log = ceil_pow2(_n); size = 1 << log; d = std::vector(2 * size, e()); lz = std::vector(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } S op(S a, S b) { return min(a, b); } S e() { return 1e18; } S mapping(F f, S x) { return f != id() ? f : x; } F composition(F f, F g) { return f != id() ? f : g; } F id() { return -1e18; } int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } ll operator[](int i) { return get(i); } void out() { for (int i = 0; i < _n; i++) cout << get(i) << " \n"[i == _n - 1]; } void err() { for (int i = 0; i < _n; i++) cerr << get(i) << " \n"[i == _n - 1]; } private: int _n, size, log; std::vector d; std::vector lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; vector f(vector a, string s) { int n = a.size(); vector ret(n); RMUQ rsuq(n); vector> ord; rep(i, 0, n) ord.emplace_back(a[i], i); sort(ord.rbegin(), ord.rend()); rep(i, 0, n) { int idx = ord[i].second; if (s[idx] == '#') continue; int l = max(0, idx - a[idx]), r = min(n, idx + a[idx] + 1); rsuq.apply(l, r, a[idx]); } rep(i, 0, n) { ll nw = rsuq.get(i); if (nw == 1e18) ret[i] = inf; else ret[i] = nw; } vector l(n, inf), r(n, inf); rep(i, 1, n) { if (ret[i] == inf) { l[i] = min(ret[i - 1] + 1, l[i - 1] + 1); } } for (int i = n - 2; i >= 0; i--) { if (ret[i] == inf) { r[i] = min(ret[i + 1] + 1, r[i + 1] + 1); } } rep(i, 0, n) ret[i] = min({ret[i], l[i], r[i]}); return ret; } int main() { int h, w; cin >> h >> w; vector s(h); rep(i, 0, h) cin >> s[i]; vector> dp(h, vector(w, 0)); for (int i = h - 2; i >= 0; i--) { auto l = f(dp[i + 1], s[i]); reverse(dp[i + 1].begin(), dp[i + 1].end()); auto r = f(dp[i + 1], s[i]); reverse(r.begin(), r.end()); reverse(dp[i + 1].begin(), dp[i + 1].end()); rep(j, 0, w) { dp[i][j] = min(l[j], r[j]); if (s[i][j] == '#') dp[i][j] = inf; } } // rep(i, 0, h) { // rep(j, 0, w) { // if (dp[i][j] == inf) // cout << "x "; // else // cout << dp[i][j] << " "; // } // cout << endl; // } rep(i, 0, w) cout << dp[0][i] << endl; }