12941 - The Game of Life   

Description

The Game of Life is devised by the British mathematician John Horton Conway.
This Game consists of a grid of cells, each of which is in one of possible state, live or dead.
For each generation, every cell interacts with its eight neighbors in the following rules:

  1. Any live cell with two or three live neighbours survives.
  2. Any dead cell with exactly three live neighbours becomes a live cell.
  3. All other live cells die in the next generation. Similarly, all other dead cells stay dead.

 

The following 2 gifs are the examples of the Game of Life: ( Black = live, White = dead )

         
Given the initial state of n×m grid of cells.
Your task is to find the state of grid of cells after T generations.

Hint:
It's possible to access the invalid index ( arr[-1][0] ) of array when counting the neighbors.
In C, this behavior may not cause any error message.
Notice the boundary cases, and you will get AC in this problem. :)

Input

Three integers n, m, T on the first line.
The following n lines consist m numbers for each lines.
There’re si1, si2, ..., sim on the ith line, representing the initial state of grid of cells.
sij denotes the state of cell at position (i,j), 0,1 mean dead and live respectively.

It’s guaranteed that:

  • 1 ≤ n, m, T ≤ 500
  • sij ∈ { 0, 1 }

Output

Print the grid of cells after T generations.
Remember ‘\n’ after the end of each line.

Sample Input  Download

Sample Output  Download

Tags




Discuss