#K52602. Largest Grass Rectangle

    ID: 29346 Type: Default 1000ms 256MiB

Largest Grass Rectangle

Largest Grass Rectangle

You are given a grid representing a field with N rows and M columns. Each cell in the grid is marked with a character. A cell marked with G represents grass, while any other character (for example, R or .) represents an obstacle.

Your task is to find the area of the largest rectangular sub-grid that consists entirely of grass cells. In other words, you need to find the maximum area of a rectangle such that every cell within it is G. The area is calculated by the formula: \( \text{area} = \text{height} \times \text{width} \).

The following constraints apply:

  • The input is read from standard input (stdin).
  • The output, which is the maximum area, is written to standard output (stdout).
  • </p>

    inputFormat

    The input consists of:

    1. A line containing two space-separated integers, \(N\) and \(M\), representing the number of rows and columns, respectively.
    2. Following this, there are \(N\) lines, each containing \(M\) characters separated by spaces. Each character is either G (grass) or another character (indicating an obstacle).

    For example:

    4 5
    G . G G G
    . G . . .
    G G G R R
    G G G G G
    

    outputFormat

    Output a single integer: the area of the largest rectangle that can be formed using only grass cells (G).

    ## sample
    4 5
    G . G G G
    . G . . .
    G G G R R
    G G G G G
    6