#K40712. Healthy Garden Transformation
Healthy Garden Transformation
Healthy Garden Transformation
You are given a garden represented by a grid of size \(n \times m\). Each cell in the grid contains an integer where 1
indicates a healthy plant and 0
indicates an unhealthy plant. Each night, every healthy plant spreads its health to its adjacent cells (up, down, left, right).
Your task is to determine the minimum number of nights required to make all plants healthy. If it is impossible to make all plants healthy (i.e. some unhealthy plants remain unreachable), output -1
.
Note: The spreading occurs simultaneously for all healthy plants each night.
inputFormat
The input is read from standard input and consists of the following:
- The first line contains two integers \(n\) and \(m\) denoting the number of rows and columns, respectively.
- The next \(n\) lines each contain \(m\) space-separated integers (either
0
or1
) representing the state of each plant in the garden.
Here, 1
means healthy and 0
means unhealthy.
outputFormat
Output a single integer to standard output - the minimum number of nights required for all plants to become healthy, or -1
if it is impossible.
3 3
1 0 1
0 0 0
1 0 1
2