#K66167. Maximal Rectangle in a Binary Matrix
Maximal Rectangle in a Binary Matrix
Maximal Rectangle in a Binary Matrix
Given a binary matrix filled with 0's and 1's, your task is to find the largest rectangle containing only 1's and return its area.
The rectangle must be axis-aligned (i.e., its sides are parallel to the matrix edges). Use an efficient algorithm to solve the problem.
Hint: You can solve this problem by leveraging the Largest Rectangle in Histogram approach.
Example:
Input: 4 5 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0</p>Output: 6
inputFormat
The first line of input contains two integers n
and m
representing the number of rows and columns in the binary matrix, respectively. The following n
lines each contain m
space-separated integers (either 0 or 1), representing the matrix.
For example:
4 5 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0
outputFormat
Output a single integer representing the area of the largest rectangle containing only 1's.
For example:
6## sample
4 5
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
6