using
System;
using
System.Collections.Generic;
class
GFG {
public
static
int
maxHist(
int
R,
int
C,
int
[] row)
{
Stack<
int
> result =
new
Stack<
int
>();
int
top_val;
int
max_area = 0;
int
area = 0;
int
i = 0;
while
(i < C) {
if
(result.Count == 0
|| row[result.Peek()] <= row[i]) {
result.Push(i++);
}
else
{
top_val = row[result.Peek()];
result.Pop();
area = top_val * i;
if
(result.Count > 0) {
area
= top_val * (i - result.Peek() - 1);
}
max_area = Math.Max(area, max_area);
}
}
while
(result.Count > 0) {
top_val = row[result.Peek()];
result.Pop();
area = top_val * i;
if
(result.Count > 0) {
area = top_val * (i - result.Peek() - 1);
}
max_area = Math.Max(area, max_area);
}
return
max_area;
}
public
static
int
maxRectangle(
int
R,
int
C,
int
[][] A)
{
int
result = maxHist(R, C, A[0]);
for
(
int
i = 1; i < R; i++) {
for
(
int
j = 0; j < C; j++) {
if
(A[i][j] == 1) {
A[i][j] += A[i - 1][j];
}
}
result = Math.Max(result, maxHist(R, C, A[i]));
}
return
result;
}
public
static
void
Main(
string
[] args)
{
int
R = 4;
int
C = 4;
int
[][] A
=
new
int
[][] {
new
int
[] { 0, 1, 1, 0 },
new
int
[] { 1, 1, 1, 1 },
new
int
[] { 1, 1, 1, 1 },
new
int
[] { 1, 1, 0, 0 } };
Console.Write(
"Area of maximum rectangle is "
+ maxRectangle(R, C, A));
}
}