Build: Loop through the grid.
If a cell is , unite it with its neighbors. Use DSU with Size Tracking.
Search: Now loop through every cell.
Test: Imagine turning this into a . It would connect its neighbors.
Sum: Look up the Leader of each neighbor. Sum the sizes of these unique leaders + (for the new cell).
The answer is the maximum sum you find.
Note: Be careful not to double-count if Up and Down already belong to the same island!