01 Matrix

Each cell of a rectangular binary matrix holds either a 0 or a 1. For every cell, report the Manhattan distance to the closest cell that holds a 0: the number of horizontal and vertical steps in a shortest path that moves only between edge-adjacent cells, where the path may pass through any cell regardless of its value. Cells that already hold a 0 have distance 0.

The values 0 and 1 in the statement are matrix entries, not distances, so a cell containing 1 can still end up with a small distance when a zero sits beside it. Diagonal neighbours are not adjacent. When the matrix contains at least one zero, every cell has a finite answer, and the returned matrix keeps the original shape.

A direct search from each cell costs O((r * c)^2) and is too slow at the stated limits. The efficient method reverses the direction: start from every zero at once and expand outward in layers. Seeding a queue with all zero cells and running breadth-first search over the four neighbours assigns each cell the length of the shortest path from the nearest zero, because the first time a cell is reached is necessarily through a minimum-length route. Initialize unvisited cells with a large sentinel, or with the input value itself, and let the queue relax them. The result is O(r * c) time and O(r * c) space for the queue and distance table.

Examples
Input: [[0]]
Output: [[0]]
Hints

01 Matrix

Each cell of a rectangular binary matrix holds either a 0 or a 1. For every cell, report the Manhattan distance to the closest cell that holds a 0: the number of horizontal and vertical steps in a shortest path that moves only between edge-adjacent cells, where the path may pass through any cell regardless of its value. Cells that already hold a 0 have distance 0.