using
System;
using
System.Collections.Generic;
class
GFG {
static
int
[][] neighbors(
int
r,
int
c,
int
N) {
int
[][] list =
new
int
[4][]{
new
int
[]{r - 1, c},
new
int
[] {r + 1, c},
new
int
[]{r, c - 1},
new
int
[] {r, c + 1}};
List<
int
[]> res =
new
List<
int
[]>();
foreach
(
int
[] x
in
list) {
if
(x[0] >= 0 && x[0] < N && x[1] >= 0 && x[1] < N) {
res.Add(x);
}
}
return
res.ToArray();
}
static
int
dfs(
int
R,
int
C,
int
index,
int
[][] grid,
int
N) {
int
ans = 1;
grid[R][C] = index;
foreach
(
int
[] x
in
neighbors(R, C, N)) {
int
nr = x[0], nc = x[1];
if
(grid[nr][nc] == 1) {
ans += dfs(nr, nc, index, grid, N);
}
}
return
ans;
}
static
int
largestPath(
int
[][] grid) {
int
N = grid.Length;
Dictionary<
int
,
int
> area =
new
Dictionary<
int
,
int
>();
int
index = 2;
for
(
int
i = 0; i < N; i++) {
for
(
int
j = 0; j < N; j++) {
if
(grid[i][j] == 1) {
area.Add(index, dfs(i, j, index, grid, N));
index++;
}
}
}
int
ans = 0;
foreach
(
int
a
in
area.Values) {
ans = Math.Max(ans, a);
}
for
(
int
i = 0; i < N; i++) {
for
(
int
j = 0; j < N; j++) {
if
(grid[i][j] == 0) {
HashSet<
int
> seen =
new
HashSet<
int
>();
foreach
(
int
[] x
in
neighbors(i, j, N)) {
int
nr = x[0], nc = x[1];
if
(grid[nr][nc] > 1) {
seen.Add(grid[nr][nc]);
}
}
int
temp = 1;
foreach
(
int
k
in
seen) {
temp += area[k];
}
ans = Math.Max(ans, temp);
}
}
}
return
ans;
}
public
static
void
Main(
string
[] args) {
int
[][] I =
new
int
[2][] {
new
int
[] { 1, 0 },
new
int
[] { 0, 1 } };
Console.WriteLine(largestPath(I));
}
}