Rectangle Painting 1 test case failed

Code : https://ide.codingblocks.com/s/361362

Approach: For every element in the grid that is black, took min of cost painting square of size 0 to max(i, j)+1 where i, j are its position.

Can someone help me in finding the issue in this code.

I have modified the above approach to check painting cost for every position even if its already white,
https://ide.codingblocks.com/s/361372.
It has passed all cases. But I’m not sure if this is the correct approach as in other doubt responses O(n^5) approach is given.

Hey @Realnam332 , Your code works fine . What is your doubt can you explain ?

When I checked similar doubts of this question, hints were given for a 4D dp solution in O(n^5) TC and since my code uses 2D dp, so I wanted to confirm that its correct and is not just passing the test cases on hackerblocks.