Imagine you're standing in a maze of 0s and 1s, and you need to find the shortest path from every cell to the nearest safe zone (0). This is exactly what the 01 Matrix problem asks you to solve!

Given an m ร— n binary matrix mat containing only 0s and 1s, your task is to calculate the minimum distance from each cell to the nearest 0. Two cells are considered adjacent if they share a common edge (up, down, left, right), and each step has a distance of 1.

Goal: Transform the matrix so that each cell contains the distance to the nearest 0.

Example: If you have a matrix like [[0,0,0],[0,1,0],[1,1,1]], the cell at position (1,1) has a distance of 1 to reach any 0, while the cell at (2,2) has a distance of 2.

Input & Output

example_1.py โ€” Basic Matrix
$ Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
โ€บ Output: [[0,0,0],[0,1,0],[0,0,0]]
๐Ÿ’ก Note: The center cell (1,1) is already surrounded by zeros at distance 1, so its value becomes 1. All other cells are already zeros.
example_2.py โ€” Complex Matrix
$ Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
โ€บ Output: [[0,0,0],[0,1,0],[1,2,1]]
๐Ÿ’ก Note: Bottom row: (2,0) and (2,2) are distance 1 from nearest zeros, while (2,1) is distance 2 from the nearest zeros.
example_3.py โ€” Single Element
$ Input: mat = [[0]]
โ€บ Output: [[0]]
๐Ÿ’ก Note: Edge case with single zero element - distance to itself is 0.

Visualization

Tap to expand
๐Ÿฅ Hospital Emergency Response Network๐Ÿฅ๐Ÿฅ1121Multi-source BFS Process:๐Ÿฅ Hospitals send signals simultaneously๐Ÿ“ก Distance 1: First wave reaches neighbors๐Ÿ“ก Distance 2: Second wave continuesโšก First signal arrival = shortest distanceWhy this works:โ€ข BFS explores level by level (shortest first)โ€ข Multiple sources = parallel explorationโ€ข O(mn) time - each cell visited once๐Ÿ”„ Each wave represents one BFS level, guaranteeing shortest distances
Understanding the Visualization
1
Emergency Alert
All hospitals (zeros) simultaneously send out emergency signals
2
First Wave
Signals reach adjacent buildings at distance 1
3
Second Wave
Signals propagate further to buildings at distance 2
4
Complete Coverage
All buildings now know their distance to nearest hospital
Key Takeaway
๐ŸŽฏ Key Insight: Multi-source BFS treats all zeros as starting points, spreading distances like ripples in water to find shortest paths efficiently in O(mn) time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(mn)

Each cell is visited exactly once during BFS traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(mn)

Queue can contain up to all cells in worst case, plus result matrix

n
2n
โšก Linearithmic Space

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 โ‰ค m, n โ‰ค 104
  • 1 โ‰ค m * n โ‰ค 104
  • mat[i][j] is either 0 or 1
  • There is at least one 0 in mat
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
42.0K Views
High Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen