<p>You are given two <code>m x n</code> binary matrices <code>grid1</code> and <code>grid2</code> containing only <code>0</code>&#39;s (representing water) and <code>1</code>&#39;s (representing land). An <strong>island</strong> is a group of <code>1</code>&#39;s connected <strong>4-directionally</strong> (horizontal or vertical). Any cells outside of the grid are considered water cells.</p>

<p>An island in <code>grid2</code> is considered a <strong>sub-island </strong>if there is an island in <code>grid1</code> that contains <strong>all</strong> the cells that make up <strong>this</strong> island in <code>grid2</code>.</p>

<p>Return the <em><strong>number</strong> of islands in </em><code>grid2</code> <em>that are considered <strong>sub-islands</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/test1.png" style="width: 493px; height: 205px;" />
<pre>
<strong>Input:</strong> grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/03/testcasex2.png" style="width: 491px; height: 201px;" />
<pre>
<strong>Input:</strong> grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
<strong>Output:</strong> 2 
<strong>Explanation: </strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid1.length == grid2.length</code></li>
	<li><code>n == grid1[i].length == grid2[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 500</code></li>
	<li><code>grid1[i][j]</code> and <code>grid2[i][j]</code> are either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
    void activateIsland(std::map<int, int>& stateMap, int island){
        int& ref = stateMap[island];
        if(ref <= 1) return;
        int state = stateMap[ref];
        while(state > 1) state = stateMap[state];
        stateMap[ref] = island;
        ref = state;
    }

    void mergeIslands(std::map<int, int>& stateMap, int island, int another){
        if(island == another) return;
        int& state = stateMap[island];
        if(state > 1){
            std::cerr << "Assert violation: reference in main island" << std::endl;
            throw std::logic_error("");
        }
        int* anotherState = &stateMap[another];
        if(*anotherState <= 1){ //value to value
            state = std::min(state, *anotherState);
            *anotherState = island;
        } else { //reference to value
            int* refState = &stateMap[*anotherState];
            while(*refState > 1){
                anotherState = refState;
                refState = &stateMap[*refState];
            }
            if(*anotherState == island) return;
            state = std::min(state, *refState);
            *refState = island;
        }
    }
public:
    int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
        int islandName = 1; //last used name
        std::map<int, int> islandState; //0 - bad, 1 - good so far, N - actually part of N
        for(size_t i = 0; i < grid2.size(); ++i){
            for(size_t j = 0; j < grid2[0].size(); ++j){
                if(grid2[i][j] == 0) continue;
                //name the island:
                int west = j>0 ? grid2[i][j-1] : 0;                
                if(int north = i>0 ? grid2[i-1][j] : 0; north != 0){
                    if(west == 0 || north == west) {
                        //continue from north
                        activateIsland(islandState, north);
                        grid2[i][j] = north;
                        if(grid1[i][j] == 0) islandState[north] = 0;
                    } else {
                        //merge islands, new island is "west"
                        mergeIslands(islandState, west, north);
                        grid2[i][j] = west;
                        if(grid1[i][j] == 0) islandState[west] = 0;
                    }
                } else if(west != 0){
                    //continue from west
                    grid2[i][j] = west;
                    if(grid1[i][j] == 0) islandState[west] = 0;
                } else {
                    //new island
                    grid2[i][j] = ++islandName;
                    islandState[islandName] = grid1[i][j];
                }
            }
        }
        //now we just count "good" islands
        int ret = 0;
        for(auto [name, status] : islandState){
            if(status == 1) ++ret;
        }
        return ret;
    }
};
103062