The english mathematician John Conway invented in 1970 the following game:
Imagine a matrix with rows and columns. The (at most, eight) positions which are at distance 1, horizontally, vertically or in diagonal are considered adjacent positions. In each moment, each position of the matrix is empty or contains a tree. The rules are:
An empty position in a moment will contain a tree at the moment if and only if at the moment had exactly three adjacent trees.
An occupied position at the moment will contain a tree at the moment if and only if at the moment had two or three adjacent trees.
Your task is to write a program that, for each given matrix, prints the matrix at the posterior moment of time.
The input consists of zero or more cases. Each case consists of a
line with
and
(two integers between 1 and 100) followed by
lines (one per row) each one with
characters: X if the position is occupied and
. if the position is empty. A line with
indicates the end of the input.
For each case, your program must print the matrix corresponding to the next moment using the same format than the one of the input. It must print a line feed after each matrix.
Salvador Roura (en: Carlos Molina)
© Jutge.org, 2006–2025.
Input
2 3 X.X .X. 2 2 XX XX 0 0
Output
.X. .X. XX XX