Problem:
Score : 700 points

Problem Statement

Snuke and Ciel went to a strange stationery store. Each of them got a transparent graph paper with H rows and W columns.

Snuke painted some of the cells red in his paper. Here, the cells painted red were 4-connected, that is, it was possible to traverse from any red cell to any other red cell, by moving to vertically or horizontally adjacent red cells only.

Ciel painted some of the cells blue in her paper. Here, the cells painted blue were 4-connected.

Afterwards, they precisely overlaid the two sheets in the same direction. Then, the intersection of the red cells and the blue cells appeared purple.

You are given a matrix of letters a_{ij} (1≤i≤H, 1≤j≤W) that describes the positions of the purple cells. If the cell at the i-th row and j-th column is purple, then a_{ij} is #, otherwise a_{ij} is .. Here, it is guaranteed that no outermost cell is purple. That is, if i=1, H or j = 1, W, then a_{ij} is ..

Find a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation described. It can be shown that a solution always exists.

Constraints

3≤H,W≤500

a_{ij} is # or ..

If i=1,H or j=1,W, then a_{ij} is ..

At least one of a_{ij} is #.

Input

The input is given from Standard Input in the following format:

H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}

Output

Print a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation, as follows:

The first H lines should describe the positions of the red cells.

The following 1 line should be empty.

The following H lines should describe the positions of the blue cells.

The description of the positions of the red or blue cells should follow the format of the description of the positions of the purple cells.

Sample Input 1

5 5
.....
.#.#.
.....
.#.#.
.....

Sample Output 1

.....
#####
#....
#####
.....

.###.
.#.#.
.#.#.
.#.#.
.....

One possible pair of the set of the positions of the red cells and the blue cells is as follows:

Sample Input 2

7 13
.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#.###.###.
.............

Sample Output 2

.............
.###########.
.###.###.###.
.###.###.###.
.###.###.###.
.###.###.###.
.............

.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#########.
.............

One possible pair of the set of the positions of the red cells and the blue cells is as follows:

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


import java.util.*;

public class Main {

    int H, W;
    boolean[][] board;
    G[][] gd;
    List<G> gl = new ArrayList<>();
    Set<Long> gs = new HashSet<>();

    void run() {
        Scanner sc = new Scanner(System.in);
        H = sc.nextInt();
        W = sc.nextInt();
        gd = new G[H + 2][W + 2];

        for (int h = 1; h <= H; h++) {
            String l = sc.next();
            for (int w = 1; w <= W; w++) {
                if (l.charAt(w - 1) == '#') {
                    gl.add(new G(w, h));
                    gs.add(1L * h * 1000 + w);
                }
            }
        }

        init();
        G s = gl.get(0);

        bfs(s.x, s.y);
        init();
        rbfs(s.x, s.y);

        printBoard();
        System.out.println();

        gd = new G[H + 2][W + 2];

        for (G g: gl) board[g.y][g.x] = false;

        bfs(s.x, s.y);
        init();

//        debug(gl);
//        for (int i = 1; i <= H + 1; i++) debug(gd[i]);

        rbfs(s.x, s.y);

        printBoard();
    }

    void init() {
        board = new boolean[H + 2][W + 2];
        Arrays.fill(board[0], true);
        Arrays.fill(board[H + 1], true);
        for (int i = 0; i < H + 2; i++) board[i][0] = board[i][W + 1] = true;
    }

    int[] dx = {-1,0,1,0};
    int[] dy = {0,-1,0,1};
    void bfs(int x, int y) {
        PriorityQueue<G> q = new PriorityQueue<>();
        q.add(new G(x, y));
        board[y][x] = true;
        while (!q.isEmpty()) {
            G cur = q.remove();
            for (int i = 0; i < 4; i++) if (!board[cur.y + dy[i]][cur.x + dx[i]]) {
                board[cur.y + dy[i]][cur.x + dx[i]] = true;
                gd[cur.y + dy[i]][cur.x + dx[i]] = new G(cur.x, cur.y);
                long hash = 1L * (cur.y + dy[i]) * 1000 + cur.x + dx[i];
                q.add(new G(cur.x + dx[i], cur.y + dy[i], gs.contains(hash) ? 0 : 1));
            }
        }
    }

    void rbfs(int x, int y) {
        for (G g: gl) {
            while (g.x != x || g.y != y) {
                if (board[g.y][g.x]) break;
                board[g.y][g.x] = true;
                g = gd[g.y][g.x];
                if (g == null) break;
            }
        }
        board[y][x] = true;
    }

    void printBoard() {
        for (int h = 1; h <= H; h++) {
            for (int w = 1; w <= W; w++) {
                System.out.print(board[h][w] ? '#' : '.');
            }
            System.out.println();
        }
    }

    class G implements Comparable<G> {
        int x, y, c;
        G(int x, int y) {
            this.x = x;
            this.y = y;
            c = 1;
        }

        G(int x, int y, int c) {
            this.x = x;
            this.y = y;
            this.c = c;
        }

        @Override
        public String toString() {
            return String.format("(x: %d, y: %d)", x, y);
        }

        @Override
        public int compareTo(G o) {
            return c - o.c;
        }
    }

    void debug(Object...os) {
        System.err.println(Arrays.deepToString(os));
    }

    public static void main(String[] args) {
        new Main().run();
    }
}