#C1183. Largest Rectangle of Trees

    ID: 41189 Type: Default 1000ms 256MiB

Largest Rectangle of Trees

Largest Rectangle of Trees

You are given a 2D grid representing a forest. Each cell in the grid is either '1' (indicating a tree) or '0' (indicating an empty space). Your task is to determine the area of the largest rectangle that is completely filled with trees. The rectangle must be formed by contiguous rows and columns.

This problem can be efficiently solved by converting every row into a histogram and then using a stack-based algorithm to compute the largest rectangular area in that histogram. The final answer is the maximum area found over all rows.

inputFormat

The input is given through standard input (stdin). The first line contains two integers n and m separated by a space, where n is the number of rows and m is the number of columns in the grid. The following n lines each contain a string of m characters (each character is either '0' or '1'), representing a row of the forest.

outputFormat

Output a single integer to standard output (stdout) representing the area of the largest rectangle composed entirely of trees.## sample

3 4
0100
1111
0110
4