#K49332. Maximum Rectangle Area of 'X'
Maximum Rectangle Area of 'X'
Maximum Rectangle Area of 'X'
You are given an N x M grid, where each cell contains either the character X
or .
. Your task is to find the area of the largest rectangular sub-grid that contains only the character X
.
The area of a sub-grid is defined as the number of cells in that rectangle. If there is no rectangle filled entirely with X
, then the answer is 0
.
Mathematical formulation:
Let f(i, j) denote the height of consecutive X
's in column j ending at row i. Then the problem reduces to finding the maximum area in the histogram of heights for each row. The largest area for a given histogram is given by:
\[ \max_{1 \leq i \leq N} \left( \text{max_area}(\{f(i,1), f(i,2), \dots, f(i,M)\}) \right) \]
inputFormat
The input is given via standard input (stdin) in the following format:
- The first line contains two space-separated integers N and M, representing the number of rows and columns respectively.
- The next N lines each contain a string of length M, where each character is either
X
or.
.
outputFormat
Output a single integer to standard output (stdout), which is the area of the largest rectangular sub-grid that contains only X
.
4 5
X.X.X
XX..X
XXXX.
XXX.X
6