#C13365. Conway's Game of Life Evolution

    ID: 42895 Type: Default 1000ms 256MiB

Conway's Game of Life Evolution

Conway's Game of Life Evolution

This problem asks you to simulate Conway's Game of Life over a given number of generations. The evolution of the grid is based on the following rules:

  • Any live cell with fewer than $$2$$ live neighbours dies (underpopulation).
  • Any live cell with $$2$$ or $$3$$ live neighbours lives on to the next generation.
  • Any live cell with more than $$3$$ live neighbours dies (overpopulation).
  • Any dead cell with exactly $$3$$ live neighbours becomes a live cell (reproduction).

You are given the initial configuration of the grid and the number of generations to simulate. Your task is to output the final state of the grid after applying the rules for the specified number of generations.

inputFormat

The first line of input contains two integers, r and c, representing the number of rows and columns of the grid, respectively.

Each of the next r lines contains c space-separated integers (either 0 or 1) representing the initial state of the grid. Here, 1 indicates a live cell and 0 indicates a dead cell.

The last line contains an integer G, which is the number of generations to simulate.

All input is read from stdin.

outputFormat

Output the final state of the grid after G generations. The output should consist of r lines, each containing c space-separated integers representing the state of the grid. All output is to be printed to stdout.

## sample
3 3
0 0 0
0 0 0
0 0 0
1
0 0 0

0 0 0 0 0 0

</p>