The game of the life

The english mathematician John Conway invented in 1970 the following game:

Imagine a matrix with nn rows and mm 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:

Your task is to write a program that, for each given matrix, prints the matrix at the posterior moment of time.

Input

The input consists of zero or more cases. Each case consists of a line with NN and MM (two integers between 1 and 100) followed by NN lines (one per row) each one with MM characters: X if the position is occupied and . if the position is empty. A line with N=M=0N = M = 0 indicates the end of the input.

Output

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.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T10:08:37.549Z

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