Can anybody find the mistake in my code?

The problem link: https://www.codechef.com/problems/MINCOLOR

My solution: https://www.codechef.com/viewsolution/60749886

Explanation:
while filling each cell in the rectangle i just check if that cell can be filled with a color(number) which is not present in the adjacent cell and is also the minimum number. Since I am filling the minimum number possible at each iteration I expected to get the correct output.
But all the test cases passed except for the 4th one and I don’t know the reason why?

Please help me…

Check the following accepted solution for hint about the reason of the failure of your code in the 4th test case.

Accepted