<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 checkArea(vector<vector<int>>& grid, int start_x, int start_y )
    {
        if ( start_x < 0 || start_x >= grid.size() || start_y < 0 || start_y >= grid.size())
        {
            return 0;
        }
        else if (grid[start_x][start_y]==0)
        {
            return 0;
        }
        else
        {
            return grid[start_x][start_y];
        }
    }
    int findZeroNeighbor(vector<vector<int>>& grid,vector<int>& size_grid,int start_x, int start_y)
    {
        vector<int> empty_size_grid(size_grid.size());
        int up = checkArea(grid,start_x-1,start_y);
        int down = checkArea(grid,start_x+1,start_y);
        int left = checkArea(grid,start_x,start_y-1);
        int right = checkArea(grid,start_x,start_y+1);
        int sum = 0;
        if( up != 0 && empty_size_grid[up-2] !=1 )
        {
            empty_size_grid[up-2]=1;
            sum+=size_grid[up-2];
        }
        if( down != 0 && empty_size_grid[down-2] !=1 )
        {
            empty_size_grid[down-2]=1;
            sum+=size_grid[down-2];
        }
        if( left != 0 && empty_size_grid[left-2] !=1 )
        {
            empty_size_grid[left-2]=1;
            sum+=size_grid[left-2];
        }
        if( right != 0 && empty_size_grid[right-2] !=1 )
        {
            empty_size_grid[right-2]=1;
            sum+=size_grid[right-2];
        }
        return sum +1;
    }
    int findZeroNeighbor1(vector<vector<int>>& grid,vector<int>& size_grid,int start_x, int start_y)
    {
        int total_area=0;
        vector<int> empty_size_grid(size_grid.size()+2);
        if(start_x > 0 && grid[start_x -1][start_y]>1)
        {
            empty_size_grid[grid[start_x -1][start_y]] = 1;
            total_area += size_grid[grid[start_x -1][start_y]-2];
        }
        if ( start_y > 0 && grid[start_x][start_y-1]>1 && empty_size_grid[grid[start_x][start_y-1]] != 1)
        {
            empty_size_grid[grid[start_x][start_y-1]] = 1;
            total_area += size_grid[grid[start_x][start_y-1]-2];
        }
        if(start_x < grid.size()-1 && grid[start_x + 1][start_y]>1 && empty_size_grid[grid[start_x+1][start_y]] != 1)
        {
            empty_size_grid[grid[start_x+1][start_y]] = 1;
            total_area += size_grid[grid[start_x+1][start_y]-2];
        }
        if(start_y < grid.size()-1 && grid[start_x][start_y+1]>1 && empty_size_grid[grid[start_x][start_y+1]] != 1)
        {
            empty_size_grid[grid[start_x][start_y+1]] = 1;
            total_area += size_grid[grid[start_x][start_y+1]-2];
        }
        return total_area+1;
    }
    int findLargestArea(vector<vector<int>>& grid, int current_index, int start_x, int start_y, int total_size)
    {
        if(start_x >= grid.size() || start_y >= grid[0].size() || start_x < 0 || start_y < 0 )
        {
            return 0;
        }
        if( grid[start_x][start_y] != 1 )
        {
            return 0;
        }
        if( grid[start_x][start_y] == 1 )
        {
            total_size++;
            grid[start_x][start_y] = current_index;
            total_size+=findLargestArea( grid,current_index,start_x + 1,start_y,0);
            total_size+=findLargestArea( grid,current_index,start_x,start_y+1,0);
            //findLargestArea( grid,current_index,start_x+1,start_y+1);
            total_size+=findLargestArea( grid,current_index,start_x-1,start_y,0);
            //findLargestArea( grid,current_index,start_x-1,start_y-1);
            total_size+=findLargestArea( grid,current_index,start_x,start_y-1,0);
            return total_size;//Done marking
        }
        return 0;
    }
    int largestIsland(vector<vector<int>>& grid) {
        vector<int> size_grid;
        int current_area_start =2;
        for( int i = 0 ; i< grid.size(); i++)
        {
            for(int j = 0;j< grid[0].size(); j++)
            {
                if(grid[i][j]==1)
                {
                    int size = findLargestArea(grid,current_area_start,i,j,0);
                    if( size )
                    {
                        current_area_start++;
                        size_grid.push_back(size);
                    }
                }
            }
        }
        if(size_grid.size()==0)
        {
            return 1;
        }
        /*
        bool nozero = true;
        int largestarea=0;
        for( int i = 0 ; i< grid.size(); i++)
        {
            for(int j = 0;j< grid[0].size(); j++)
            {
                if(grid[i][j]==0)
                {
                    nozero =false;
                    int size = findZeroNeighbor(grid,size_grid, i,j);
                    if(size > largestarea )
                    {
                        largestarea = size;
                    }
                }
            }
        }
        if(nozero)
        {
            largestarea = size_grid[0];
        }
        return largestarea;*/
         int maxArea = 0;
         maxArea = size_grid[0];
        for(int i = 0; i < grid.size(); i++)
        {
            for(int j = 0; j < grid[0].size(); j++)
            {
                if(grid[i][j] == 0)
                {
                    vector<int> proccessed;
                    int area = 1;
                    if(i > 0 && grid[i-1][j] > 1)
                    {
                        proccessed.push_back(grid[i-1][j]);
                        area += size_grid[grid[i-1][j]-2];
                    }
                    if(j > 0 && grid[i][j-1] > 1 && find(proccessed.begin(), proccessed.end(), grid[i][j-1]) == proccessed.end())
                    {
                        proccessed.push_back(grid[i][j-1]);
                        area += size_grid[grid[i][j-1]-2];
                    }
                    if(i < grid.size()-1 && grid[i+1][j] > 1 && find(proccessed.begin(), proccessed.end(), grid[i+1][j]) == proccessed.end())
                    {
                        proccessed.push_back(grid[i+1][j]);
                        area += size_grid[grid[i+1][j]-2];
                    }
                    if(j < grid[0].size()-1 && grid[i][j+1] > 1 && find(proccessed.begin(), proccessed.end(), grid[i][j+1]) == proccessed.end())
                    {
                        area += size_grid[grid[i][j+1]-2];
                    }
                    maxArea = max(maxArea, area);
                }              
            }
        }
        return maxArea;
    }
};

75290