I cannot figure it out why is this code wrong?

import java.util.*;
import java.util.Arrays;
import java.math.BigInteger;
public class Main {
static int ans = 0,count=0;
static boolean flag = false;
static int[] dx = {-1,0,1,0};
static int[] dy = {0,-1,0,1};
static void expandThePond(int[][] pond ,int row ,int col){
if(row>=pond.length || col>=pond.length || row<0 || col<0)
{
ans = Math.max(ans, count);
return;
}
if(flag == true && pond[row][col]==0)
{
ans = Math.max(ans,count);
return;
}
if(flag == false && pond[row][col]==0)
{
flag = true;
count=1;
for (int i = 0; i < 4; i++) {
expandThePond(pond, row+dx[i], col+dy[i]);
}
count = 1;
flag = false;
}
if(pond[row][col]==1)
{
count++;
pond[row][col] = 0;
for (int i = 0; i < 4; i++) {

            expandThePond(pond, row+dx[i], col+dy[i]);    
        }
        pond[row][col] = 1;
    }
    expandThePond(pond, row+1, col);
    expandThePond(pond, row, col+1);
}
public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    int[][] pond = new int[n][m];
    for (int i = 0; i < pond.length; i++) {
        for (int j = 0; j < pond[i].length; j++) {
            pond[i][j] = sc.nextInt();
        }
    }
    expandThePond(pond,0,0);
    System.out.println(ans);
}

}

try to run this code in your ide please and help me with this code

Your logic is incorrect.
First find the connected components and their size in the graph. Then iterate over the pond. If you find a zero anywhere, imagine you put a 1 there. Then new size of this pond will be 1 + size of neighbour ponds. Take care a neighbour of cell may cover more than 1 sides of the current cell, so only add such neighbour once.

I didn’t get you actually can you send me any link so that i can understand this better or can you explain this more?

I know this thing can be done by flood fill but i am unable to think the logic here

Do a flood fill. Assign all the members of one pond a value unique to that pond. Then store size of pond along with its unique id. Then iterate over matrix again. When you find a zero, see all its neighbours and put them in an unordered_set. Then add their pond sizes + 1. Keep updating max accordingly.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.