using
System;
using
System.Collections.Generic;
class
GFG
{
static
List<List<List<
int
>>> dp;
static
int
countPaths = 0;
static
bool
isPerfectSquare(
int
n)
{
double
sr = Math.Sqrt(n);
return
((sr - Math.Floor(sr)) == 0);
}
static
void
countUniquePaths(
int
[,] a,
int
m,
int
n,
int
ans)
{
dp[0][0].Add(a[0, 0]);
for
(
int
i = 1; i < m; i++)
{
a[i, 0] *= a[i - 1, 0];
dp[i][0].Add(a[i, 0]);
}
for
(
int
i = 1; i < n; i++)
{
a[0, i] *= a[0, i - 1];
dp[0][i].Add(a[0, i]);
}
for
(
int
i = 1; i < m; i++)
{
for
(
int
j = 1; j < n; j++)
{
List<
int
> top = dp[i - 1][j];
List<
int
> left = dp[i][j - 1];
List<
int
> curr =
new
List<
int
>();
for
(
int
k = 0;
k < top.Count; k++)
{
curr.Add(top[k] * a[i, j]);
}
for
(
int
k = 0;
k < left.Count; k++)
{
curr.Add(left[k] * a[i, j]);
}
dp[i][j] = curr;
}
}
foreach
(
int
i
in
dp[m - 1][n - 1])
{
if
(isPerfectSquare(i))
{
countPaths++;
}
}
}
public
static
void
Main()
{
int
M = 3, N = 4;
int
[,] mat = { { 1, 2, 3, 1 },
{ 3, 1, 2, 4 },
{ 2, 3, 1, 1 } };
dp =
new
List<List<List<
int
>>>();
for
(
int
i = 0; i < 105; i++)
{
dp.Add(
new
List<List<
int
>>());
for
(
int
j = 0; j < 105; j++)
dp[i].Add(
new
List<
int
>());
}
countUniquePaths(mat, M, N, 1);
Console.Write(countPaths);
}
}