I want to find neighbors of a member of an array

I want to make"LOLO Game". A brief description of LOLO game: The game starts with a matrix filled with random numbers. The player selects a cell. After an input, all the cells which follow these criteria will collapse to the selected cell:

1.have the same number as the selected cell.
2.have an unbroken path(not diagonally) of similar cells from the selected cell.

After the collapsion, all cells follow gravity and empty cells formed on top are filled again with random numbers. Scoring is based on the number of cells collapsed.

Example: if the matrix is:

1 2 3 4 5
1 1 1 1 3
5 1 3 2 4
3 2 1 1 1
1 1 1 1 1

and the player selects [0][1] ie 1

the resultant matrix will be :

x x x x 5
1 x 3 4 3
5 2 3 2 4
3 2 1 1 1
1 1 1 1 1

where x represents the newly inserted random numbers

I need to perform this on a 5x5 matrix. The newly entered number is in the range 1-largest number in the matrix. At the first, we have numbers 1-5 and only one 6 in the last line. Q1. How do I identify the cells to be deleted? Q2. How to implement the gravity effect after deletion?

I have built the 5x5 matrix with random numbers.

//  _0_1_2_3_4
// 0|_|_|_|_|_|
// 1|_|_|_|_|_|
// 2|_|_|_|_|_|
// 3|_|_|_|_|_|
// 4|_|_|_|_|_|

void drawBoad(board[sizex][sizey]) 
{ 
    int r1=rand() % 5 ,r2=4;  //for one 6
    int firstboard[5][5]={{rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1},
                 {rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1},
                 {rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1},                      
                 {rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1},                      
                 {rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1,rand() % 5 + 1}};
firstboard[r1][r2]=6;  //for one 6
for (int y = 0; y < sizey; y++)     //This draws the body 
{ 
    cout << y << "|"; 
    for (int x = 0; x < sizex; x++) 
{           
  cout <<Board[x][y]<< "|";
}
    cout << endl;