using
System;
using
System.Collections.Generic;
public
class
GFG
{
static
int
OverlapCount(List<List<
int
>> mat1, List<List<
int
>> mat2,
int
shiftX,
int
shiftY)
{
int
n = mat1.Count, count = 0;
for
(
int
i = 0; i < n; i++)
{
for
(
int
j = 0; j < n; j++)
{
if
(i + shiftX >= 0 && j + shiftY >= 0 &&
i + shiftX < n && j + shiftY < n &&
mat1[i][j] * mat2[i + shiftX][j + shiftY] == 1)
{
count++;
}
}
}
return
count;
}
static
int
LargestOverlap(List<List<
int
>> mat1, List<List<
int
>> mat2)
{
int
n = mat1.Count, result = 0;
List<List<
int
>> dir =
new
List<List<
int
>> {
new
List<
int
> { 1, 1 },
new
List<
int
> { -1, 1 },
new
List<
int
> { 1, -1 },
new
List<
int
> { -1, -1 } };
for
(
int
d = 0; d < 4; d++)
{
for
(
int
i = 0; i < n; i++)
{
for
(
int
j = 0; j < n; j++)
{
int
res = OverlapCount(mat1, mat2, i * dir[d][0], j * dir[d][1]);
result = Math.Max(result, res);
}
}
}
return
result;
}
public
static
void
Main()
{
List<List<
int
>> mat1 =
new
List<List<
int
>> {
new
List<
int
> { 0, 0, 0, 0 },
new
List<
int
> { 0, 0, 0, 0 },
new
List<
int
> { 0, 1, 0, 0 },
new
List<
int
> { 0, 1, 0, 0 }
};
List<List<
int
>> mat2 =
new
List<List<
int
>> {
new
List<
int
> { 0, 0, 1, 0 },
new
List<
int
> { 0, 0, 1, 0 },
new
List<
int
> { 0, 0, 0, 0 },
new
List<
int
> { 0, 0, 1, 0 }
};
Console.WriteLine(LargestOverlap(mat1, mat2));
}
}