Relay activation time

Write codeMedium30 minAmazon style

Your task

A grid of size m x n holds values: 0 = empty, 1 = a fresh resource, 2 = an active relay. Each minute, every active relay activates all empty cells that are 4-directionally adjacent to it, turning them into relays (value 2). Fresh resources (1) are obstacles and are never activated and never block; they simply stay 1. Return the number of minutes until there are no more empty (0) cells reachable from any relay, i.e. the last minute at which any empty cell becomes a relay. If there are no empty cells at all, return 0. Cells that remain unreachable empty after the process make this impossible, return -1 in that case.

Examples

relay_spread(grid: list[list[int]]) -> int
Example 1
Inputrelay_spread([[2, 0, 1], [0, 0, 1], [1, 0, 0]])
Output4