01 Matrix

Try to solve the 01 Matrix problem.

Statement

Given an m×nm \times n binary matrix, mat, find the distance from each cell to the nearest 00. The distance between two adjacent cells is 11. Cells to the left, right, above, and below the current cell will be considered adjacent.

Constraints:

  • 1≤1 \leq mat.row , mat.col≤102\leq 10^2

  • 1≤1 \leq mat.row * mat.col ≤104\leq 10^4

  • mat[i][j] ∈{0,1}\in \{0, 1\}

  • There is at least one 00 in mat.

Example

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy