We’re preparing your current view and syncing the latest data.
You are given an m x n binary matrix mat, where each element is either 0 or 1. For each cell containing 1, find the distance to the nearest 0 cell. The distance between two adjacent cells is 1. Return the updated matrix where each cell containing 1 is replaced by its distance to the nearest 0. Cells that are 0 remain 0.
An m x n binary matrix.
An m x n matrix where each cell is the distance to the nearest 0.
1 <= m, n <= 10^4; 1 <= m * n <= 10^4; mat[i][j] is either 0 or 1.
Example 1
Input
[[0,0,0],[0,1,0],[1,1,1]]
Output
[[0,0,0],[0,1,0],[1,2,1]]
Explanation
Distances for cells with 1 are updated to their nearest zero distances.