<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 {
    static constexpr array<pair<int, int>, 4> dirs = {{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}};
    int islandId{1};
    unordered_map<int, int> islandIdMap{};
public:
    
    void addNeighbors(auto& grid, auto& deque, auto& visited, int r, int c)
    {
        auto n = size(grid);
        for (auto [ro, co] : dirs)
        {
            auto nr = r+ro;
            auto nc = c+co;
            if (nr >= 0 && nr < n && nc >= 0 && nc < n && grid[nr][nc] && !visited.contains(nr*n+nc))
            {
                visited.insert(nr*n+nc);
                deque.push_back(make_pair(nr, nc));
            }
        }
    }
    
    void annotateIsland(auto& grid, int row, int col)
    {
        auto n = size(grid);
        unordered_set<int> visited{};
        visited.insert(row*n+col);
        deque<pair<int, int>> queue{make_pair(row, col)};
        auto islandSize{0};
        while (!queue.empty())
        {
            auto [r, c] = queue.front();
            queue.pop_front();
            islandSize++;
            addNeighbors(grid, queue, visited, r, c);
        }
        visited.clear();
        visited.insert(row*n+col);
        queue.push_back(make_pair(row, col));
        while (!queue.empty())
        {
            auto [r, c] = queue.front();
            queue.pop_front();
            grid[r][c] = islandSize;
            addNeighbors(grid, queue, visited, r, c);
            islandIdMap[r*n+c] = islandId;
        }
        islandId++;
    }
    
    void annotateIslandSizes(vector<vector<int>>& grid)
    {
        for (auto r = 0; r < size(grid); r++)
        {
            for (auto c = 0; c < size(grid[0]); c++)
            {
                if (grid[r][c] == 1)
                    annotateIsland(grid, r, c);
            }
        }
    }
    
    int maximimExpansion(auto& grid, int r, int c)
    {
        auto n = size(grid);
        unordered_set<int> visited{};
        auto bridgedIsland = 1;
        for (auto [ro, co] : dirs)
        {
            auto nr = r+ro;
            auto nc = c+co;
            if (nr >= 0 && nr < n && nc >= 0 && nc < n && grid[nr][nc] && !visited.contains(islandIdMap[nr*n+nc]))
            {
                visited.insert(islandIdMap[nr*n+nc]);
                bridgedIsland += grid[nr][nc];
            }
        }
        return bridgedIsland;
    }
        
    int largestIsland(vector<vector<int>>& grid) {
        annotateIslandSizes(grid);
        auto largest{0};
        for (auto r = 0; r < size(grid); r++)
        {
            for (auto c = 0; c < size(grid); c++)
            {
                if (!grid[r][c])
                {
                    largest = max(largest, maximimExpansion(grid, r, c));
                }
                else
                {
                    largest = max(largest, grid[r][c]);
                }
            }
        }
        return largest;
    }
};
263109