This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
using u128 = __uint128_t;
const int x[4] = {1, 0, -1, 0};
const int y[4] = {0, -1, 0, 1};
const ll mod =1e9 + 7;
const int mxn = 1e6 + 5, mxq = 1e5 + 5, sq = 300, mxm = 1e6 + 5;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
// <3;
int n, m;
vt<vt<char>>v;
vt<vt<int>>comp;
int idcomp = 0;
int in[mxn + 1], dp[mxn + 1];
vt<pii>adj[mxn + 1];
bool inside(int cx, int cy){
    return(cx >= 0 && cy >= 0 && cx < n && cy < m);
}
void dfs(int cx, int cy){
    comp[cx][cy] = idcomp;
    for(int i = 0; i < 4; i++){
        int px = cx + x[i], py = cy + y[i];
        if(inside(px, py) && !comp[px][py] && v[px][py] == '#')dfs(px, py);
    }
}
void solve(){
    cin >> n >> m;
    v.resize(n, vt<char>(m));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> v[i][j];
        }
    }
    comp.resize(n + 1, vt<int>(m, 0));
    
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(comp[i][j] == 0 && v[i][j] == '#'){
                idcomp++;
                dfs(i, j);
            }
        }
    }
    
    for(int i = 0; i < m; i++){
        int last = n;
        for(int j = n - 1; j >= 0; j--){
            if(v[j][i] == '#'){
                if(comp[last][i] != comp[j][i]){
                    adj[comp[last][i]].pb(mpp(comp[j][i], last - j - 1)); in[comp[j][i]]++;
                }
                last = j;
            }
        }
    }
    
    priority_queue<pii, vt<pii>, greater<pii>>pq;
    for(int i = 1; i <= idcomp; i++)dp[i] = inf;
    pq.push(mpp(dp[0], 0));
    while(!pq.empty()){
        auto [dd, u] = pq.top(); pq.pop();
        if(dp[u] != dd)continue;
        for(auto [v, w]: adj[u]){
            if(dp[v] > dp[u] + w){
                dp[v] = dp[u] + w;
                pq.push(mpp(dp[v], v));
            }
        }
    }
    vt<vt<char>>res(n, vt<char>(m));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            res[i][j] = '.';
        }
    } 
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(v[i][j] == '#'){
                int offset = dp[comp[i][j]];
                res[i + offset][j] = '#';
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cout << res[i][j];
        }
        cout << "\n";
    } 
    
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("SODOCLA.INP", "r", stdin);
    //freopen("SODOCLA.INP", "w", stdout);
    int tt; tt = 1;
    while(tt--){
        solve();
    }
    return(0);
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:81:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   81 |         auto [dd, u] = pq.top(); pq.pop();
      |              ^
Main.cpp:83:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   83 |         for(auto [v, w]: adj[u]){
      |                  ^| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |