find minimum number of clicks to make monochromatic grid
A grid of size NxM where each cell of the grid is filled with some color. Find minimum number of clicks to make monochromatic grid (having one color). Problem reference (https://www.urionlinejudge.com.br/challenges/en/4b85c9eff62f37e3d169f4f62b67a6b655a6ccb1.html )
When the player clicks on any cell of the grid of color α, the cell in the top-leftmost corner of the grid, of color β, receives the color α, but not only it: all those cells which are connected to the source by paths which use only the colors α or β also receive the color α.
The connection between cells should be considered only in the horizontal and vertical directions to form the paths. For example, when the player clicks on the cell highlighted in the figure to the left, the grid receives the coloring of the figure to the right.
Input Description The first line is number of test cases.The next line of the input consists of 2 integers N and M (1 ≤ N ≤ 4, 1 ≤ M ≤ 5), which represent respectively the number of lines and the number of columns of the grid. The N lines following describe the initial configuration of the grid, representing each color by an integer between 0 and 9. Output Description Print a line containing a single integer that represents the minimum number of clicks that the player must do in order to make the grid monochromatic.
3 4 5 00162 30295 45033 01837 4 5 01234 12345 23456 34567 4 5 01234 34567 67890 90123
10 7 12