<p>You are given an <code>n x n</code> binary matrix <code>grid</code>. You are allowed to change <strong>at most one</strong> <code>0</code> to be <code>1</code>.</p>

<p>Return <em>the size of the largest <strong>island</strong> in</em> <code>grid</code> <em>after applying this operation</em>.</p>

<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,0],[0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1],[1,0]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>Change the 0 to 1 and make the island bigger, only one island with area = 4.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1],[1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Can&#39;t change any 0 to 1, only one island with area = 4.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
private:
    bool inBounds(vector<vector<int>>& grid, const int i, const int j) {
        return i >= 0 && i < grid.size() && j >= 0 && j < grid[0].size();
    }

    int floodFill(vector<vector<int>>& grid, int i, int j, int id) {
        // Perform a DFS, if we hit a value != 1, break it.
        std::queue<std::pair<int, int>> index_queue{};
        index_queue.push({i, j});
        int island_size{0};
        while(index_queue.size()) {
            const auto [k, l] = index_queue.front();
            index_queue.pop();

            if(!inBounds(grid, k, l)) {
                continue;
            }
            if(grid[k][l] != 1) {
                continue;
            }
            grid[k][l] = id;
            island_size++;

            std::vector<std::pair<int, int>> dirs{{-1,0}, {1,0},{0,-1},{0,1}};
            for(auto [x, y] : dirs) {
                index_queue.push({x+k, y+l});
            }
        }
        return island_size;
    }

public:
    int largestIsland(vector<vector<int>>& grid) {
        int island_id{2};
        int largest_island{0};
        std::unordered_map<int, int> island_size{};
        std::vector<std::pair<int, int>> boundary_waters{};

        // Loop over every cell.
        for(int i{0}; i<grid.size(); i++) {
            for(int j{0}; j<grid[0].size(); j++) {
                // If we hit an already claimed island, continue.
                if(grid[i][j] > 1) {
                    continue;
                } else if(grid[i][j]==1) {
                // If we hit a new island, flood fill it, get its size and add to a map.
                    island_size[island_id] = floodFill(grid, i, j, island_id);
                    largest_island = max(largest_island, island_size[island_id]);
                    island_id++;
                } else if(grid[i][j]==0) {
                // If we hit a 0 (ocean) check its neighbors: two unique neighbors > 0? track for later.
                    std::set<int> islands_found{};
                    std::vector<std::pair<int, int>> dirs{{-1,0}, {1,0},{0,-1},{0,1}};
                    for(auto [k, l] : dirs) {
                        if(!inBounds(grid, i+k, j+l)) {
                            continue;
                        }
                        if(grid[i+k][j+l] > 0) {
                            islands_found.insert(grid[i+k][j+l]);
                        }
                    }
                    // Need to take unique and if >= 2 we track this as a boundary water
                    if(islands_found.size() >= 2) {
                        boundary_waters.push_back({i, j});
                    }
                }
            }
        }

        for(const auto [i, j] : boundary_waters) {
            if(!inBounds(grid, i, j)) {
                continue;
            }
            std::set<int> islands_found{};
            std::vector<std::pair<int, int>> dirs{{-1,0}, {1,0},{0,-1},{0,1}};
            for(auto [k, l] : dirs) {
                if(!inBounds(grid, i+k, j+l)) {
                    continue;
                }
                if(grid[i+k][j+l] > 0) {
                    islands_found.insert(grid[i+k][j+l]);
                }
            }
            // Need to take unique and if >= 2 we track this as a boundary water
            if(islands_found.size() >= 2) {
                int combined_island{1};
                for(const auto id : islands_found) {
                    combined_island += island_size[id];
                }
                largest_island = max(largest_island, combined_island);
            }
        }
        int add_one = boundary_waters.size()==0 && largest_island != grid.size()*grid[0].size();
        return largest_island + add_one;
    }
};
270196