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≤50\leq 50

  • 1≤1 \leq mat.row * mat.col ≤2500\leq 2500

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

Continue your learning journey with a 14-day free trial.

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