Fractals

Consider an n×mn \times m matrix of chars MM, which may include ‘.’, ‘X’, ‘p’ and ‘n’. Define the “negative” of MM as the matrix result of replacing each ‘.’ by ‘X’, each ‘X’ by ‘.’, each ‘p’ by ‘n’, and each ‘n’ by ‘p’. For instance, the negative of

XpX     .n.
.n.     XpX

is

We can use MM to create some kind of fractals, by repeatedly replacing each character cc of the current matrix by an n×mn \times m matrix, with these rules:

With the example above, after one step we get

XXXXpXXXX
XXX.n.XXX
....n....
...XpX...

and after two steps we get

XXXXXXXXXXXXXpXXXXXXXXXXXXX
XXXXXXXXXXXX.n.XXXXXXXXXXXX
XXXXXXXXX....n....XXXXXXXXX
XXXXXXXXX...XpX...XXXXXXXXX
.............n.............
............XpX............
.........XXXXpXXXX.........
.........XXX.n.XXX.........

Can you simulate this process kk times?

Input

Input consists of several cases. Every case begins with nn, mm and kk, followed by an n×mn \times m matrix MM as explained above. Assume that nn and mm are between 1 and 100, and k1k \ge 1.

Output

Print kk matrices for each case: the result after one step, two steps, etc. Separate these matrices by blank lines. End each case with 10 asteriscs. When printing the results, replace each ‘p’ by ‘X’, and each ‘n’ by ‘.’. With the given cases, no result will have more than 10610^6 chars.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T11:32:33.461Z

© Jutge.org, 2006–2026.
https://jutge.org