using System;
using System.Collections.Generic;
class GFG {
static int validCoord(int x, int y, int n, int m)
{
if (x < 0 || y < 0) {
return 0;
}
if (x >= n || y >= m) {
return 0;
}
return 1;
}
static void bfs(int n, int m, int[,] data, int X, int Y, int color)
{
int[,] vis = new int[101,101];
List<Tuple<int, int>> obj = new List<Tuple<int, int>>();
obj.Add(new Tuple<int,int>( X, Y ));
vis[X,Y] = 1;
while (obj.Count > 0)
{
Tuple<int, int> coord = obj[0];
int x = coord.Item1;
int y = coord.Item2;
int preColor = data[x,y];
data[x,y] = color;
obj.RemoveAt(0);
if (validCoord(x + 1, y, n, m) == 1
&& vis[x + 1,y] == 0
&& data[x + 1,y] == preColor)
{
obj.Add(new Tuple<int,int>(x + 1, y));
vis[x + 1,y] = 1;
}
if (validCoord(x - 1, y, n, m) == 1
&& vis[x - 1,y] == 0
&& data[x - 1,y] == preColor)
{
obj.Add(new Tuple<int,int>(x - 1, y));
vis[x - 1,y] = 1;
}
if (validCoord(x, y + 1, n, m) == 1
&& vis[x,y + 1] == 0
&& data[x,y + 1] == preColor)
{
obj.Add(new Tuple<int,int>(x, y + 1));
vis[x,y + 1] = 1;
}
if (validCoord(x, y - 1, n, m) == 1
&& vis[x,y - 1] == 0
&& data[x,y - 1] == preColor)
{
obj.Add(new Tuple<int,int>(x, y - 1));
vis[x,y - 1] = 1;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(data[i,j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
static void Main() {
int n, m, x, y, color;
n = 8;
m = 8;
int[,] data = {
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 0, 0 },
{ 1, 0, 0, 1, 1, 0, 1, 1 },
{ 1, 2, 2, 2, 2, 0, 1, 0 },
{ 1, 1, 1, 2, 2, 0, 1, 0 },
{ 1, 1, 1, 2, 2, 2, 2, 0 },
{ 1, 1, 1, 1, 1, 2, 1, 1 },
{ 1, 1, 1, 1, 1, 2, 2, 1 },
};
x = 4;
y = 4;
color = 3;
bfs(n, m, data, x, y, color);
}
}