Problem
You are given a map mountain of size , where is the length and width of the map. The map consists of trees and rocks, and at time , forest fires occur simultaneously at various locations on the map.
The spread of the fire follows these rules:
- Fires spread one cell at a time in all four directions (up, down, left, right) simultaneously every second.
- Fires can spread to
treesbut cannot spread torocks. - The process continues until the fire can no longer spread.
Write a function solution that takes the map mountain, which contains the locations of trees (0), rocks (1), and initial fires (2), and returns the time it takes for the fire to completely spread to all reachable areas.
Constraints
mountainis an square grid, where is a natural number between 3 and 1,000 inclusive.- Every element in
mountainis either 0 (tree), 1 (rock), or 2 (initial fire).
Test Cases
| Test Case Number | mountain | return |
|---|---|---|
| 1 | [[2,1,0,1],[0,1,1,1],[1,0,1,1],[0,2,0,2]] | 1 |
| 2 | [[0,1,0,1,1],[0,0,1,0,2],[0,0,0,0,1],[1,0,0,0,0],[0,0,0,0,0]] | 7 |
| 3 | [[2,1,1,1],[1,1,1,0],[1,1,1,1],[1,1,2,1]] | 0 |
Explanation for Test Case 1

- At , the fire is already located at some parts of the map. After 1 second, the fire has spread completely to all reachable areas.

Explanation for Test Case 2

- At , the fire starts in specific locations on the map. After 7 seconds, the fire has spread completely to all reachable areas.

Explanation for Test Case 3
- At , the fire cannot spread further because it is surrounded by rocks. Therefore, the function returns 0.