This page is still under construction.

Parts of this page are still being built. What you see may change.

Zombie Virus

Interview

Time limit2sMemory limit1024 MB

Summary
Two viruses spread one cell per hour from fixed sources on a grid; when both reach a cell within the same hour, virus 3 forms there. Count final cells of each type.
Level

Medium6 of 10

Topics
BFS, Graph, Simulation, Implementation
Solved
No attempts yet

Problem

There is a village in the shape of an NN x MM grid. One day a zombie virus breaks out and spreads quickly through the village. An investigation of the virus revealed three types, numbered 11, 22, and 33.

The viruses behave as follows.

  • Viruses 11 and 22 have a low mortality rate but are highly contagious. They spread simultaneously to the villages adjacent up, down, left, and right, and take 1 hour to fully infect a village.
  • A virus can spread to another village only after the current village is fully infected, and it does not invade a village that another virus has fully infected.
  • If a virus of a different type arrives at a village before one virus has fully infected it, virus 33 is created.
  • Virus 33 has a high mortality rate and, being weakly contagious, no longer spreads from the village it infects.
  • A village holding a cure cannot be infected.

Villages infected with virus 11 and virus 22 have appeared. Given that the viruses spread as far as they can, find how many villages are infected with virus 11, virus 22, and virus 33, respectively.

Input

The first line gives NN (2≤N≤1 0002≤N≤1\,000) and MM (2≤M≤1 0002≤M≤1\,000).

Starting from the second line, NN lines each give the state of MM villages. A village state is one of the following.

  • −1-1: a village holding a cure
  • 00: a village not yet infected
  • 11: a village infected with virus 11
  • 22: a village infected with virus 22

Exactly one village infected with virus 11 and exactly one village infected with virus 22 are given.

Output

Print the numbers of villages infected with virus 11, virus 22, and virus 33, separated by spaces, on one line.

Examples2

  1. Example 1

    Input
    4 4
    0 0 0 0
    0 1 0 0
    0 0 0 0
    0 0 0 2
    
    Expected output
    10 3 3
    
  2. Example 2

    Input
    7 9
    0 0 0 0 0 0 0 0 0
    0 0 0 2 0 0 -1 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 -1 0 0 0 1 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 -1 0 0
    0 0 0 0 0 0 0 0 0
    
    Expected output
    25 29 6