Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/matritsa-hayot-oyini

Game of Life: one step

Medium 1000 ms 128 MB 58%
Share

A field of size $n \times m$ is given: every cell is alive ($1$) or dead ($0$). The neighbours of a cell are the $8$ cells around it (cells outside the field do not count). By the rules of Conway's Game of Life, after one step:

  • a live cell with $2$ or $3$ live neighbours stays alive, otherwise it dies;
  • a dead cell with exactly $3$ live neighbours comes to life, otherwise it stays dead.

All cells change at the same time: neighbours are counted in the state before the step. Print the field after one step.

A step showing how the cells' states change

Input

The first line contains two positive integers $n$ and $m$. Each of the next $n$ lines contains $m$ numbers ($0$ or $1$).

Output

$n$ lines with $m$ space-separated numbers ($0$ or $1$) each.

Note

$1 \le n, m \le 200$
$a_{i,j} \in \{0, 1\}$

Examples

Input #1
3 3
0 1 0
0 1 0
0 1 0
Output #1
0 0 0
1 1 1
0 0 0
Input #2
4 4
1 1 0 0
1 1 0 0
0 0 1 1
0 0 1 1
Output #2
1 1 0 0
1 0 0 0
0 0 0 1
0 0 1 1

Sign in to submit a solution

Signing up is free and takes a minute.