Skip to main content
LeetCode 221, Medium. Topics: Array, Dynamic Programming, Matrix. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

Given an m x n binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.

Examples

Example 1
Example 2

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 300
  • matrix[i][j] is ‘0’ or ‘1’.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Grind, NeetCode All.
Last modified on August 25, 2026