<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 {
public:
    vector<vector<int>>* Grid;
    queue<pair<int,int>> q;    
    int ilNo = 1;
    int currIlSize = 0;
    int maxX=0,maxY=0;

    void addIfValidPoint(vector<pair<int,int>>& v, int i, int j) {
        if (i >=0 && j>=0 && i < maxX && j < maxY && (*Grid)[i][j] !=0 ) {
            v.emplace_back(make_pair(i,j));            
        }
    }

    vector<pair<int,int>> getNeighbors(pair<int,int> p) {
        vector<pair<int,int>> retVal;

        addIfValidPoint(retVal, p.first, p.second+1);
        addIfValidPoint(retVal, p.first, p.second-1);
        addIfValidPoint(retVal, p.first+1, p.second);
        addIfValidPoint(retVal, p.first-1, p.second);

        return retVal;


    }
    void bfs() {

        while (!q.empty()) {
            auto neighbors = getNeighbors(q.front());
            q.pop();
        
            for (auto neighbor: neighbors) {
                if ((*Grid)[neighbor.first][neighbor.second] == 1) {
                    q.push(neighbor);
                    (*Grid)[neighbor.first][neighbor.second] = ilNo;
                    currIlSize++;
                }
            }
        }
    }

    int largestIsland(vector<vector<int>>& grid) {
        Grid = &grid;
        map<int,int> sizeMap;
        int retVal;

        bool zeroExists = false;
        maxX= grid.size();
        maxY= grid[0].size();

        for (int i=0; i < grid.size(); i++)
        for (int j=0; j < grid[0].size(); j++) {
            if (grid[i][j] == 1) {
                ilNo++;
                currIlSize = 1;

                grid[i][j] = ilNo;
                q.push({i,j});
                                
                bfs();
                sizeMap[ilNo] = currIlSize;
            } else if(grid[i][j] == 0) {
                zeroExists = true;
            }
        }

        if (sizeMap.size() == 1) {
            return zeroExists ? sizeMap[2]+1 : sizeMap[2];
        } else {
            int maxSize = -1;
            for (int i=0; i < grid.size(); i++)
            for (int j=0; j < grid[0].size(); j++) {
                if (grid[i][j] == 0) {
                    set<int> s;
                    for (auto neighbor: getNeighbors({i,j})) {
                        s.insert((*Grid)[neighbor.first][neighbor.second]);
                    }

                    int size = 1;
                    for (auto s1: s) {
                        size += sizeMap[s1];
                    }
                    maxSize = max(maxSize, size);
                }
            }
            return maxSize;

        }

        return retVal;
        
    }
};
170971