


An island is a 4-directionally (North, South, East, West) connected group of 1s.
Input: 'grid' = [[1,0],
[0,1]]
Output: 3
Explanation:
We can change the 0 at (0,1) to 1 and get an island of size 3.
The first line of each test case contains an integer 'n', representing the number of rows and columns in 'grid'.
Each of the next 'n' lines contain 'n' elements each denoting the values of the grid.
Return the largest size of the island in the grid after applying the given operation at most once.
You do not need to print anything, it has already been taken care of. Just implement the given function.
Let us group the islands by changing the 1s to their index and increment the index. All islands in a particular group have the same index. We store the index and the corresponding size in a map and keep track of the maximum size until now.
1 0 1 -> 2 0 3
0 1 1 -> 0 3 3
1 0 1 -> 4 0 3Now, we traverse each 0 in the grid and find its adjacent group and add up their areas.
For the 0 at (0,1), we get area = m[2]+m[3]+1 = 1+4+1 = 6
For the 0 at (1,0), we get area = m[2]+m[3]+m[4]+1 = 1+4+1+1 = 7
For the 0 at (2, 1), we get area = m[4]+m[3]+1 = 1+4+1 = 6
We add 1 to account for the converted island.
In the end, we are left with the required answer.
The idea behind the solution is :