542. 01 Matrix

Practice 01 Matrix for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Given an m x n binary matrix mat, return a matrix of the same size where each cell holds the distance to the nearest 0 in mat. Two cells sharing an edge are at distance 1.

Example

mat = [[0,0,0],
       [0,1,0],
       [1,1,1]]  ->  [[0,0,0],
                      [0,1,0],
                      [1,2,1]]

Constraints