#C3985. Conway's Game of Life Simulation

    ID: 47472 Type: Default 1000ms 256MiB

Conway's Game of Life Simulation

Conway's Game of Life Simulation

You are given an initial grid representing cells in Conway's Game of Life. Each cell is either alive ('*') or dead ('.'). The grid evolves in discrete iterations according to the following rules:

  • If a cell is alive and has 2 or 3 live neighbors, it remains alive; otherwise, it dies.
  • If a cell is dead and has exactly 3 live neighbors, it becomes alive; otherwise, it remains dead.

These rules can be written in LaTeX as follows:

For a live cell: it survives if $2 \leq N \leq 3$, otherwise it dies.
For a dead cell: it becomes live if $N = 3$.

Your task is to simulate the game for k iterations on each given grid and output the number of live cells remaining at the end.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
n m k
row1
row2
...
rown
... (repeated for T test cases)

Where:

  • T is the number of test cases.
  • For each test case, the first line contains three integers n, m, and k representing the number of rows, the number of columns, and the number of iterations, respectively.
  • Each of the next n lines contains a string of length m consisting only of the characters . (dead cell) and * (live cell), representing the initial grid configuration.

outputFormat

For each test case, output a single integer on a new line representing the number of live cells remaining after k iterations. The output is written to standard output (stdout).

## sample
1
3 3 1
.*.
*.*
.*.
4

</p>