<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:
    int numOnes, connectedComp, n;
    vector<vector<int> > vis;
    vector<vector<int> > connectedCount;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    bool isValid(int r, int c) {
        if (r >= 0 && r < n && c >= 0 && c < n) {
            return true;
        }
        return false;
    }
    void dfs2(vector<vector<int>>& grid, int r, int c) {
        if (!isValid(r,c))
            return;
        if (isValid(r,c) && vis[r][c] == connectedComp) {
            connectedCount[r][c] = numOnes;
        }
        int x,y;
        for(int i = 0; i < 4; i++) {
            x = r + dx[i];
            y = c + dy[i];
            if (isValid(x, y) && vis[x][y]==connectedComp && connectedCount[x][y] != numOnes) {
                //cout<<x<<y<<endl;
                connectedCount[x][y] = numOnes;
                dfs2(grid, x, y);
            }
        }
    }
    void dfs(vector<vector<int>>& grid, int r, int c) {
        if (!isValid(r,c))
            return;
        if (vis[r][c] == 0) {
            vis[r][c] = connectedComp;
        }
        int x,y;
        for(int i = 0; i < 4; i++) {
            x = r + dx[i];
            y = c + dy[i];
            if (isValid(x, y) && vis[x][y]==0 && grid[x][y]==1) {
                //cout<<x<<y<<endl;
                vis[x][y] = connectedComp;
                numOnes++;
                dfs(grid, x, y);
            }
        }
    }
    int largestIsland(vector<vector<int>>& grid) {
        n = grid.size();
        int i, j;
        //init
        for(i = 0; i < n; i++) {
            vector<int> v;
            for(j = 0; j < n; j++) {
                v.push_back(0);
            }
            vis.push_back(v);
            connectedCount.push_back(v);
        }
        int ans = 0;
        //pre-calc
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                if (grid[i][j] == 1 && vis[i][j] == 0) {
                    connectedComp++;
                    numOnes = 1;
                    dfs(grid, i, j);
                    dfs2(grid, i, j);
                    ans = max(ans, numOnes);
                }
            }
        }
        //cout<<ans<<endl;
        int x, y, a, b, cnt;
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                //cout<<i<<" "<<j<<endl;
                if (grid[i][j] == 0) {
                    cnt = 1;
                    unordered_map<int, bool> visMap;
                    //cout<<i<<j<<endl;
                    for(int k = 0; k < 4; k++) {
                        x = i + dx[k];
                        y = j + dy[k];
                        //cout<<k<<endl;
                        if (isValid(x,y) && connectedCount[x][y] > 0
                        && visMap[vis[x][y]] == false) {
                            visMap[vis[x][y]] = true;
                            cnt += connectedCount[x][y];
                        }
                    }
                    ans = max(ans, cnt);
                }
            }
        }
        return ans;
    }
};
// 0000000
// 0111100
// 0100100
// 1010100
// 0100100
// 0100100
// 0111100

// 0000000
// 0
181603