Find Matching tiles in a 2D Object Grid

I’m trying to find the (best) way to find matching similar objects in a given 2D Object Array like the following.

1 2 1 1
1 2 2 2 <<---
1 2 1 1

Starting with the Block shown by the Arrow, the algorithm would find all the tiles with the same ID. In this cas there would be 5 and return them in the Matches List.

My problem is that I cannot find a way to properly tag Blocks that I have visited, so I end up having 50 tiles matching.

Maybe it’s not the proper way to do it; i’ve also found a ‘Fill’ algorithm, but I don’t think it can apply here.

Any help would be MUCHOS appreciated

Thanks

// A Block Class

public class Block : MonoBehaviour {
	// ID of this block, can be 0 (empty), 1, 2, 3
	public int ID;
	public float X;
	public float Y;

}

// A Board

public class Board : MonoBehaviour {
	// Size of the board
	public int Width = 20;
	public int Height = 10;
	
	// 2D grid
	public Block[,] Grid;

	Grid = new Block[Width,Height*2];

	void CheckTiles (Block startingBlock) {
		// List of Blocks that would match my startingBlock
		List<Block> Matches = new List<Block>();
		
		// Scanning from block towards X-Axis, same row 
		for (int _x = (int)checkBlock.X; _x < Width; _x++) {
			// We found the same block, add it to our Matches list
			if (Grid[_x, startingBlock.Y].ID = startingBlock.ID) { 
				Matches.Add ( Grid[_x, startingBlock.Y]; 
			} else {
				break;
			}
		}
		// Scanning from block backwards X-Axis, same row
		// Scanning from block upwards Y-Axis, same column
		// Scanning from block downwards Y-axis, same column
		return Matches;
	}


}

A fill algorithm is exactly what you want. Specifically a “breadth first search” (BFS). This Red Blob Games article is the best I’ve found on the topic. Even though it’s ultimately talking about A*, it first goes through the simpler algorithms such as BFS. It’s a very simple algorithm and the article explains it very clearly.