Skip to main content
Back to problems
Leetcode
Medium
Arrays
Matrices
Stacks
Dynamic Programming
Amazon
Microsoft
Google
Maximal Rectangle

Find the area of the largest rectangle containing only 1s in a binary matrix.

Acceptance 100%
Problem Statement

Given a binary matrix of characters '0' and '1', find the area of the largest axis-aligned rectangle that contains only 1s.

You may choose any contiguous set of rows and columns, but every cell inside the rectangle must be 1.

Your task is to return the maximum possible area.

Input Format

  • A binary matrix matrix with m rows and n columns.
  • Each cell contains either '0' or '1'.

Output Format

  • Return a single integer: the maximum rectangle area consisting entirely of 1s.

Constraints

  • 1 <= m, n
  • Cells are binary characters '0' or '1'.
  • A valid rectangle must be contiguous and axis-aligned.
Examples
Sample cases returned by the problem API.

Example 1

Input

matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]

Output

6

Explanation

The largest rectangle of 1s has area 6, formed by rows 2-3 and columns 1-3 (0-indexed in a 2x3 block of 1s).

Example 2

Input

matrix = [["0"]]

Output

0

Explanation

There is no rectangle containing only 1s.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.