cswork / OAOpen practice workspace

PinterestOA #5

Bubble Explosion

Estimated MediumJudge-ready

Topics (AI-labelled)Grid SearchSimulation

On the board, bubbles of the same color that are adjacent up, down, left, or right form connected groups. A bubble is eligible to explode if it has at least two neighbors of the same color; all eligible bubbles and their same-colored neighbors explode simultaneously. Then the remaining bubbles in each column fall down, and empty cells are represented by 0. The process ends after a single explosion.

Input

The first line contains rows cols; each of the next rows lines contains cols color integers. Source constraints: 1≤rows,cols≤100, 1≤color≤10⁴.

Output

Output the rows×cols matrix after the explosion and the fall, with each row space-separated.

Example 1

Input:

4 4
3 1 2 1
1 1 1 4
3 1 2 2
3 3 3 4

Output:

0 0 0 1
0 0 0 4
0 0 2 2
3 0 2 4

Example 2

Input:

2 3
1 2 2
1 1 2

Output:

0 0 0
0 0 0

Example 3

Input:

1 1
1

Output:

1

Editorial

Loading solution…