using
System;
using
System.Collections.Generic;
class
GFG
{
static
int
row = 6;
static
int
col = 8;
static
int
cal(
int
[]hist,
int
n)
{
Stack<
int
> s =
new
Stack<
int
>();
int
max_area = 0;
int
tp;
int
area_with_top;
int
i = 0;
while
(i < n)
{
if
(s.Count == 0 || hist[s.Peek()] <= hist[i])
s.Push(i++);
else
{
tp = s.Peek();
s.Pop();
area_with_top = hist[tp] * (s.Count == 0 ? i :
i - s.Peek() - 1);
if
(max_area < area_with_top)
max_area = area_with_top;
}
}
while
(s.Count == 0 ==
false
)
{
tp = s.Peek();
s.Pop();
area_with_top = hist[tp] * (s.Count == 0 ? i :
i - s.Peek() - 1);
if
(max_area < area_with_top)
max_area = area_with_top;
}
return
max_area;
}
static
int
largestMatrix(
int
[,]a)
{
int
[,]dp =
new
int
[row, col];
for
(
int
i = 0; i < col; i++)
{
int
cnt = 0;
for
(
int
j = row - 1; j >= 0; j--)
{
dp[j, i] = 0;
if
(a[j, i] == 1)
{
cnt++;
dp[j, i] = cnt;
}
else
{
cnt = 0;
}
}
}
int
ans = -1;
for
(
int
i = 0; i < row; i++)
{
int
[]hist =
new
int
[col];
for
(
int
j = 0; j < col; j++)
{
hist[j] = dp[i, j];
}
ans = Math.Max(ans, cal(hist, col));
}
for
(
int
i = 0; i < col; i++)
{
int
cnt = 0;
for
(
int
j = row - 1; j >= 0; j--)
{
dp[j, i] = 0;
if
(a[j, i] == 0)
{
cnt++;
dp[j, i] = cnt;
}
else
{
cnt = 0;
}
}
}
for
(
int
i = 0; i < row; i++)
{
int
[]hist =
new
int
[col];
for
(
int
j = 0; j < col; j++)
{
hist[j] = dp[i, j];
}
ans = Math.Max(ans, cal(hist, col));
}
return
ans;
}
public
static
void
Main(String[] args)
{
int
[,]a = {{ 1, 1, 0, 1, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 1, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 1, 0, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 0 },
{ 1, 0, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 1 }};
Console.WriteLine(largestMatrix(a));
}
}