#C11226. Game of Life: Next Generation

    ID: 40519 Type: Default 1000ms 256MiB

Game of Life: Next Generation

Game of Life: Next Generation

In this problem, you are given a two-dimensional grid representing the board of Conway's Game of Life. Each cell is either live (1) or dead (0). Your task is to compute the next state of the board based on the following rules:

  • A live cell with fewer than two live neighbors dies, as if by underpopulation.
  • A live cell with two or three live neighbors lives on to the next generation.
  • A live cell with more than three live neighbors dies, as if by overpopulation.
  • A dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

You must update the board in-place and then output the new board configuration.

The board dimensions are constrained by \(1 \le m, n \le 25\), where \(m\) is the number of rows and \(n\) is the number of columns. Each cell is guaranteed to be either 0 or 1.

inputFormat

The first line contains two space-separated integers \(m\) and \(n\) indicating the number of rows and columns, respectively.

This is followed by \(m\) lines, each containing \(n\) space-separated integers (either 0 or 1) representing the board.

outputFormat

Output the board's next state in \(m\) lines. Each line should contain \(n\) space-separated integers representing the cells of the board.

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

1 0 1 0 1 1 0 1 0

</p>