#include <cstdio>
#include <memory.h>
#include <tuple>
#include <cassert>
#include <algorithm>
using namespace std;

const int N = 505;
char F[N][N];

int num[N][N];
int par[N * N];
int rnk[N * N];
pair<int, int> topmost[N * N];

int get(int x) {
    return (x == par[x]) ? x : (par[x] = get(par[x]));
}

void merge(int a, int b) {
    a = get(a);
    b = get(b);
    if (a == b)
        return;
    if (rnk[a] > rnk[b]) {
        swap(a, b);
    }
    if (rnk[a] == rnk[b])
        rnk[b]++;
    topmost[b] = min(topmost[b], topmost[a]);
    par[a] = b;
}

int vy[] = {1, 0, -1, 0};
int vx[] = {0, 1, 0, -1};

int was[N][N];

int n, m;
int DFS(int y, int x) {
    was[y][x] = true;
    int ans = 1;
    for (int v = 0; v < 4; v++) {
        int ty = y + vy[v];
        int tx = x + vx[v];
        if (ty < 0 || ty >= n || tx < 0 || tx >= m)
            continue;
        if (F[ty][tx] == '.' || F[ty][tx] == 'A')
            continue;
        if (!was[ty][tx])
            ans += DFS(ty, tx);
    }
    return ans;
}

void put(int y, int x) {
    assert(y >= 0 && y < n && x >= 0 && x < m);
    assert(F[y][x] != '#');
    F[y][x] = 'A';
    for (int v = 0; v < 4; v++) {
        int ty = y + vy[v];
        int tx = x + vx[v];
        if (ty >= 0 && ty < n && tx > 0 && tx < m && (F[ty][tx] == 'A' || F[ty][tx] == '#'))
            merge(num[y][x], num[ty][tx]);
    }
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%s", F[i]);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            num[i][j] = i * m + j;
            par[num[i][j]] = num[i][j];
            topmost[num[i][j]] = make_pair(i, j);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int v = 0; v < 4; v++) {
                int ti = i + vy[v];
                int tj = j + vx[v];
                if (ti < 0 || tj < 0 || ti >= n || tj >= m)
                    continue;
                if (F[ti][tj] == '#' && F[i][j] == '#')
                    merge(num[ti][tj], num[i][j]);
            }
        }
    }

    for (int j = 0; j < m; j++) {
        put(0, j);
    }

    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j < m; j++) {
            if (F[i][j] != '#')
                continue;
            while (true) {
                int c = get(num[i][j]);
                pair<int, int> pr = topmost[c];
                int y, x;
                tie(y, x) = pr;
                if (y == 0)
                    break;
                else {
                    put(y - 1, x);
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (F[i][j] == '.')
                F[i][j] = 'B';
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%c", ".#"[F[i][j] == 'A' || F[i][j] == '#']);
        }
        printf("\n");
    }
    printf("\n");
    int cnt = 0;
    pair<int, int> any(n - 1, 0);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%c", ".#"[F[i][j] == 'B' || F[i][j] == '#']);
            cnt += F[i][j] == 'B' || F[i][j] == '#';
        }
        printf("\n");
    }
    int rcnt = DFS(any.first, any.second);
    assert(cnt == rcnt);
    /*for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (F[i][j] == '#' && !was[i][j])
                assert(false);
            if (F[i][j] == 'B' && !was[i][j])
                F[i][j] = '.';
        }
    }
    memset(was, 0, sizeof(was));
    cnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%c", ".#"[F[i][j] == 'B' || F[i][j] == '#']);
            cnt += F[i][j] == 'B' || F[i][j] == '#';
        }
        printf("\n");
    }
    rcnt = DFS(any.first, any.second);
    assert(cnt == rcnt);*/
}


Measured memory_bytes: 18432