Rotting Oranges

Medium
Graphs Array BFS Matrix

Grid: 0=empty, 1=fresh, 2=rotten. Each minute rotten spreads to adjacent fresh. Min minutes?

Constraints

m, n ≤ 10

Examples

Example 1:
Input: 2 1 1 1 1 0 0 1 1
Output: 4
Example 2:
Input: 2 1 1 0 1 1 1 0 1
Output: -1
▲ Console

Install Talent Arabia

Get instant access to jobs and career tools on your device.