field. Each cell holds one of three values:
- 0: the spot is empty;
- 1: a healthy computer;
- 2: an infected computer.
Every minute the virus jumps from each infected computer to its healthy neighbours above, below, left and right (not diagonally).
Return how many minutes pass until no healthy computer is left. If some computer can never be infected, return -1.field=[[1, 1, 0], [2, 1, 1], [0, 0, 1]] → 3
Minute 1: the neighbours of (1,0), cells (0,0) and (1,1), get infected. Minute 2: (0,1) and (1,2). Minute 3: (2,2). Everyone is infected after 3 minutes.
field=[[2, 0, 1]] → -1
An empty cell separates the healthy computer from the infected one, so the virus never reaches it.
field=[[0, 2], [2, 0]] → 0
There are no healthy computers from the start, so no waiting is needed.
field[i][j] is 0, 1 or 2field=[[1, 1, 0], [2, 1, 1], [0, 0, 1]]
3
Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.