'I want to replace a group of numbers close to each other with value -1 in java bidimensional array

I need to know how to remove a group of neighboring elements that have the same value from a bidimensional array and replace them with value -1.

For example if i have the following matrix with dimension 5 rows x 5 columns: Matrix 5x5

I would like to know how to replace this group of close elements with the same value 4: Elements to be replaced

Not involving elements which don't form a group like the 4 in the first column.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source