#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int maxn = 2005;
string s[maxn];
string r[maxn];
int col[maxn][maxn];
int n, m;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
void dfs(int x, int y, int c) {
col[x][y] = c;
for (int dir = 0; dir < 4; ++dir) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || s[nx][ny] == '.' || col[nx][ny])
continue;
dfs(nx, ny, c);
}
}
const int N = maxn * maxn;
vector<pair<int, int>> g[N];
int dist[N];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> o;
void dickstra(int u) {
for (int i = 0; i < N; ++i) {
dist[i] = 1e9;
}
dist[u] = 0;
o.emplace(dist[u], u);
while (!o.empty()) {
int sdist = o.top().first;
u = o.top().second;
o.pop();
if (sdist != dist[u])
continue;
for (auto e : g[u]) {
int v = e.first;
int w = e.second;
int ndist = dist[u] + w;
if (ndist >= dist[v])
continue;
dist[v] = ndist;
o.emplace(dist[v], v);
}
}
}
signed main() {
#ifdef LOCAL
assert(freopen("c.in", "r", stdin));
#endif
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> s[i];
r[i] = string(m, '.');
}
int cc = 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!col[i][j] && s[i][j] == '#')
dfs(i, j, cc++);
}
}
for (int j = 0; j < m; ++j) {
int to = 0, dst = -1;
for (int i = n - 1; i >= 0; --i) {
++dst;
if (s[i][j] == '.')
continue;
g[to].emplace_back(col[i][j], dst);
to = col[i][j];
dst = -1;
}
}
dickstra(0);
//for (int i = 0; i < n; ++i) {
//for (int j = 0; j < m; ++j) {
//cerr << col[i][j];
//}
//cerr << '\n';
//}
//for (int i = 0; i < cc; ++i) {
//cerr << dist[i] << ' ';
//}
//cerr << '\n';
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (s[i][j] == '#') {
int c = col[i][j];
int sh = dist[c];
assert(i + sh < n && r[i + sh][j] == '.');
r[i + sh][j] = '#';
}
}
}
for (int i = 0; i < n; ++i) {
cout << r[i] << '\n';
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwp1c2luZyBsZCA9IGxvbmcgZG91YmxlOwoKY29uc3QgaW50IG1heG4gPSAyMDA1OwpzdHJpbmcgc1ttYXhuXTsKc3RyaW5nIHJbbWF4bl07CmludCBjb2xbbWF4bl1bbWF4bl07CmludCBuLCBtOwoKY29uc3QgaW50IGR4W10gPSB7LTEsIDAsIDEsIDB9Owpjb25zdCBpbnQgZHlbXSA9IHswLCAtMSwgMCwgMX07CnZvaWQgZGZzKGludCB4LCBpbnQgeSwgaW50IGMpIHsKICBjb2xbeF1beV0gPSBjOwogIGZvciAoaW50IGRpciA9IDA7IGRpciA8IDQ7ICsrZGlyKSB7CiAgICBpbnQgbnggPSB4ICsgZHhbZGlyXTsKICAgIGludCBueSA9IHkgKyBkeVtkaXJdOwogICAgaWYgKG54IDwgMCB8fCBueCA+PSBuIHx8IG55IDwgMCB8fCBueSA+PSBtIHx8IHNbbnhdW255XSA9PSAnLicgfHwgY29sW254XVtueV0pCiAgICAgIGNvbnRpbnVlOwogICAgZGZzKG54LCBueSwgYyk7CiAgfQp9Cgpjb25zdCBpbnQgTiA9IG1heG4gKiBtYXhuOwp2ZWN0b3I8cGFpcjxpbnQsIGludD4+IGdbTl07CmludCBkaXN0W05dOwoKcHJpb3JpdHlfcXVldWU8cGFpcjxpbnQsIGludD4sIHZlY3RvcjxwYWlyPGludCwgaW50Pj4sIGdyZWF0ZXI8cGFpcjxpbnQsIGludD4+PiBvOwp2b2lkIGRpY2tzdHJhKGludCB1KSB7CiAgZm9yIChpbnQgaSA9IDA7IGkgPCBOOyArK2kpIHsKICAgIGRpc3RbaV0gPSAxZTk7CiAgfQogIGRpc3RbdV0gPSAwOwogIG8uZW1wbGFjZShkaXN0W3VdLCB1KTsKICB3aGlsZSAoIW8uZW1wdHkoKSkgewogICAgaW50IHNkaXN0ID0gby50b3AoKS5maXJzdDsKICAgIHUgPSBvLnRvcCgpLnNlY29uZDsKICAgIG8ucG9wKCk7CiAgICBpZiAoc2Rpc3QgIT0gZGlzdFt1XSkKICAgICAgY29udGludWU7CiAgICBmb3IgKGF1dG8gZSA6IGdbdV0pIHsKICAgICAgaW50IHYgPSBlLmZpcnN0OwogICAgICBpbnQgdyA9IGUuc2Vjb25kOwogICAgICBpbnQgbmRpc3QgPSBkaXN0W3VdICsgdzsKICAgICAgaWYgKG5kaXN0ID49IGRpc3Rbdl0pCiAgICAgICAgY29udGludWU7CiAgICAgIGRpc3Rbdl0gPSBuZGlzdDsKICAgICAgby5lbXBsYWNlKGRpc3Rbdl0sIHYpOwogICAgfQogIH0KfQoKc2lnbmVkIG1haW4oKSB7CiNpZmRlZiBMT0NBTAogIGFzc2VydChmcmVvcGVuKCJjLmluIiwgInIiLCBzdGRpbikpOwojZW5kaWYKICBjaW4gPj4gbiA+PiBtOwogIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICBjaW4gPj4gc1tpXTsKICAgIHJbaV0gPSBzdHJpbmcobSwgJy4nKTsKICB9CiAgaW50IGNjID0gMTsKICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyArK2opIHsKICAgICAgaWYgKCFjb2xbaV1bal0gJiYgc1tpXVtqXSA9PSAnIycpCiAgICAgICAgZGZzKGksIGosIGNjKyspOwogICAgfQogIH0KICBmb3IgKGludCBqID0gMDsgaiA8IG07ICsraikgewogICAgaW50IHRvID0gMCwgZHN0ID0gLTE7CiAgICBmb3IgKGludCBpID0gbiAtIDE7IGkgPj0gMDsgLS1pKSB7CiAgICAgICsrZHN0OwogICAgICBpZiAoc1tpXVtqXSA9PSAnLicpCiAgICAgICAgY29udGludWU7CiAgICAgIGdbdG9dLmVtcGxhY2VfYmFjayhjb2xbaV1bal0sIGRzdCk7CiAgICAgIHRvID0gY29sW2ldW2pdOwogICAgICBkc3QgPSAtMTsKICAgIH0KICB9CiAgZGlja3N0cmEoMCk7CiAgLy9mb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgLy9mb3IgKGludCBqID0gMDsgaiA8IG07ICsraikgewogICAgICAvL2NlcnIgPDwgY29sW2ldW2pdOwogICAgLy99CiAgICAvL2NlcnIgPDwgJ1xuJzsKICAvL30KICAvL2ZvciAoaW50IGkgPSAwOyBpIDwgY2M7ICsraSkgewogICAgLy9jZXJyIDw8IGRpc3RbaV0gPDwgJyAnOwogIC8vfQogIC8vY2VyciA8PCAnXG4nOwogIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICBmb3IgKGludCBqID0gMDsgaiA8IG07ICsraikgewogICAgICBpZiAoc1tpXVtqXSA9PSAnIycpIHsKICAgICAgICBpbnQgYyA9IGNvbFtpXVtqXTsKICAgICAgICBpbnQgc2ggPSBkaXN0W2NdOwogICAgICAgIGFzc2VydChpICsgc2ggPCBuICYmIHJbaSArIHNoXVtqXSA9PSAnLicpOwogICAgICAgIHJbaSArIHNoXVtqXSA9ICcjJzsKICAgICAgfQogICAgfQogIH0KICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgY291dCA8PCByW2ldIDw8ICdcbic7CiAgfQp9Cg==