#C10207. Maximal Rectangular Island

    ID: 39387 Type: Default 1000ms 256MiB

Maximal Rectangular Island

Maximal Rectangular Island

Given a 2D grid of characters representing land (denoted by '1') and water (denoted by '0'), your task is to find the area of the largest rectangular island. An island is defined as a submatrix consisting entirely of '1's that forms a rectangle. The area of an island is the number of cells containing '1' within that rectangle.

This problem can be solved using a histogram-based method. For each row, the histogram of consecutive '1's is computed. Then, using a stack-based algorithm, the largest rectangle in the histogram is determined. Formally, if \(h[i]\) represents the height of consecutive '1's at column \(i\), the area of a rectangle with width computed between appropriate indices is given by:

$$Area = h[i] \times (right[i] - left[i] - 1)$$

inputFormat

The input is read from standard input (stdin). The first line contains two integers m and n (\(1 \leq m,n \leq 1000\)) representing the number of rows and columns in the grid, respectively. This is followed by m lines, each containing n space-separated characters ('1' or '0').

outputFormat

Output a single integer to standard output (stdout), which denotes the area of the largest rectangular island found in the grid.

## sample
5 6
1 0 1 0 0 1
1 0 1 1 1 1
1 1 1 1 1 0
0 0 0 1 0 0
1 1 0 0 0 1
6

</p>