<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>

int dx[] = { +1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
class Solution {

public:
	int mx = 510;
	vector<int>adj[510];
	int vis[510][510];

	int n, m;
	int c;

	vector<pair<int, int>>v1;

	bool isValid(int x, int y)
	{
		if (x >= 0 && x < n && y >= 0 && y < m)
		{
			return 1;
		}
		return 0;
	}

	void dfs(int x, int y, vector<vector<int>>& v)
	{
		vis[x][y] = 1;
		c++;
		v1.push_back({x, y});

		//cout<<"dfs -->> "<<x<<"  "<<y<<"      "<<c<<endl;

		for (int i = 0; i < 4; i++)
		{
			int x1 = x + dx[i];
			int y1 = y + dy[i];

			if (isValid(x1, y1) && v[x1][y1] == 1 && vis[x1][y1] == 0)
			{
				dfs(x1, y1, v);

			}

		}
	}

	int largestIsland(vector<vector<int>>& v) {
		int i, j, k;
		n = v.size();
		m = n;
		int ans = 0;

		map<pair<int, int>, pair<int, int>> parentMap;


		for (i = 0; i < n; i++)
		{
			for (j = 0; j < n; j++)
			{
				if (v[i][j] == 1)
				{
					adj[i].push_back(j);
				}
			}
		}

		memset(vis, 0, sizeof(vis));

		for (i = 0; i < n; i++)
		{
			for (j = 0; j < n; j++)
			{
				if (v[i][j] == 1 && vis[i][j] == 0)
				{
					//cout<<"start -->> "<<i<<"  "<<j<<endl;

					c = 0;
					v1.clear();
					dfs(i, j, v);

					ans = max(ans, c);

					for (k = 0; k < v1.size(); k++)
					{
						int x1 = v1[k].first;
						int y1 = v1[k].second;

						vis[x1][y1] = c;
						parentMap[ {x1, y1}] = {i, j};

						//cout<<x1<<" "<<y1<<"      "<<c<<endl;
					}
					//cout<<endl;


				}
			}
		}

		for (i = 0; i < n; i++)
		{
			for (j = 0; j < n; j++)
			{
				if (v[i][j] == 0)
				{
					//cout << "zero  :  " << i << "   " << j << endl;
					int sum = 0;
					map<pair<int, int>,int> aga;


					for (k = 0; k < 4; k++)
					{
						int x1 = i + dx[k];
						int y1 = j + dy[k];

						if (isValid(x1, y1) && v[x1][y1] == 1 )
						{
							//cout << x1 << "   " << y1 << "        " << vis[x1][y1] << endl;

							auto it = parentMap.find({x1, y1});
							if (it != parentMap.end())
							{
								int parentX = it->second.first;
								int parentY = it->second.second;

								if (aga[ {parentX, parentY}] != 1)
								{
									sum += vis[x1][y1];
								}


								aga[ {parentX, parentY}] = 1;



							}


						}

						ans = max(ans, sum + 1);

					}

					cout << endl;

				}
			}
		}




		return ans;




	}
};
135534