#K35617. Maximal Rectangle in a Binary Matrix
Maximal Rectangle in a Binary Matrix
Maximal Rectangle in a Binary Matrix
You are given a binary matrix where each element is either 0
or 1
. Your task is to find the area of the largest rectangle containing only 1's. A rectangle is defined by consecutive rows and columns and its area is given by \(\text{area} = \text{height} \times \text{width}\).
The matrix is provided as input with its dimensions, and you must output the maximum rectangular area formed solely by 1's.
For example, consider the matrix:
1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0
The maximal rectangle of 1's has an area of 6.
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
- The first line contains an integer
T
, the number of test cases. - For each test case, the first line contains two integers
n
andm
representing the number of rows and columns of the matrix. - Then follow
n
lines, each containingm
space-separated integers (either 0 or 1) representing the matrix.
If n
is 0, the matrix is empty and the answer is 0.
outputFormat
For each test case, output a single line with one integer representing the maximum area of a rectangle formed only by 1's. The output should be written to standard output (stdout).
## sample2
4 5
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
2 2
1 1
1 1
6
4
</p>