mat is a grid with m rows and n columns, and every cell holds a 0 or a 1. A step moves to the cell directly above, below, left or right (never diagonally), and every cell may be stepped on, whatever it holds.
Return a grid of the same size where each cell holds the fewest steps it takes to get from that cell to any cell holding a 0. A cell that holds a 0 is 0 steps from one.
The top-right corner is 2 steps from the 0 at the end of the bottom row (down twice), and 3 steps from the other 0, so it gets 2. The third cell of the bottom row is one step from the 0 on its right and two steps from the other one, so it gets 1.
There is only one 0, so every cell holds its distance from the top-left corner.
Constraints
m == mat.lengthn == mat[i].length1 <= m, n <= 10⁴1 <= m * n <= 10⁴mat[i][j] is 0 or 1.mat holds at least one 0.Searching outward from every 1 on its own repeats the same work again and again. Turn it around: the 0s already know their answer, so start from them.