01 Matrix

Try to solve the 01 Matrix problem.


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.


  • 11 \leq mat.row , mat.col102\leq 10^2

  • 11 \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.


Create a free account to view this lesson.

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