Few test cases passing

code: https://ide.codingblocks.com/s/562606

I traverse through the grid and whenver I find a 0 I change it to 1 and apply dfs on the grid after the call finishes I backtrack and change it to 0 again and check the answer produced by the recursive call.

Is there any way this problem can be submitted using recursion only?

hello @raghav007

no , with pure recursion its not possible to pass all test cases.

u need to use union -find datatstructure to solve this problem efficiently

I was reading online about this problem and found out that flood fill algorithm is used to solve it, can you provide a link to a solution with dsu