#K66647. Maximal Rectangle

    ID: 32467 Type: Default 1000ms 256MiB

Maximal Rectangle

Maximal Rectangle

You are given a binary matrix with n rows and m columns. Your task is to compute the area of the largest rectangle containing only 1's.

The problem can be reduced to finding the largest rectangle in a histogram for each row of the matrix. For each row, you accumulate the height of consecutive 1's and calculate the maximum rectangular area using the formula \(A = h \times w\), where \(h\) is the height and \(w\) is the width.

If the matrix is empty or no valid rectangle exists, the output should be 0.

inputFormat

The first line contains two integers n and m, representing the number of rows and columns respectively. Each of the next n lines contains m integers (either 0 or 1) separated by spaces, representing the binary matrix.

outputFormat

Output a single integer which is the area of the largest rectangle composed only of 1's.## sample

4 5
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
6