#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()); vector lb(n), rb(n); int id = 0; rep(i, 0, n) { if (s[i] == '#') id = i + 1; lb[i] = id; } id = n - 1; for (int i = n - 1; i >= 0; i--) { if (s[i] == '#') id = i - 1; rb[i] = id; } rep(i, 0, n) { int idx = ord[i].second; if (s[idx] == '#') continue; int l = max({0, idx - a[idx], lb[idx]}); int r = min({n, idx + a[idx] + 1, rb[idx] + 1}); rsuq.apply(l, r, a[idx]); } rep(i, 0, n) { ll nw = rsuq.get(i); if (nw >= 1e18 / 2) ret[i] = inf; else ret[i] = nw; } rep(i, 1, n) { if (s[i] != '#') ret[i] = min(ret[i], ret[i - 1] + 1); } for (int i = n - 2; i >= 0; i--) { if (s[i] != '#') ret[i] = min(ret[i], ret[i + 1] + 1); } 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 v = f(dp[i + 1], s[i]); rep(j, 0, w) dp[i][j] = v[j]; } // rep(i, 0, h) { // rep(j, 0, w) { // if (dp[i][j] >= inf / 2) // cerr << "x "; // else // cerr << dp[i][j] << " "; // } // cerr << endl; // } rep(i, 0, w) cout << dp[0][i] << endl; }