Expand the Pond , i know the logic but cannot implement the logic

I thought of traversing the complete matrix where their is 0 , at that place run a bfs and count the maximum pond length but i think it will be high time complexity so sir can you suggest a better approach bcz mine is not time efficient .

Just do Floodfill and mark the connected area with unique numbers.
For ex -:

1 0 0 0 1
1 0 1 1 1
0 1 0 0 0

Then after flood fill it will look like this -:

2 0 0 0 3
2 0 3 3 3
0 4 0 0 0

So now all you have to do is to check for every zero to extend your area.
Check in all four direction ( L R U D ) , and add that to set to avoid duplicates.
Then traverse the set and check which one makes the biggest area expansion.

You can refer this code for better understanding -:

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.