<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p>

<p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]
]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;]
]
<strong>Output:</strong> 3
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 300</code></li>
	<li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

class Solution {
public:
    #define F first
    #define S second
    int m,n;
    int cell[310][310];
    int vis[310][310];
    using state=pair<int,int>;
    int dx[4]={-1,0,1,0};
    int dy[4]={0,-1,0,1};

    bool valid(int x,int y){
        if(x<0 || x>=m || y<0 || y>=n || cell[x][y]==0) return false;
        return true;
    }

    vector<state> neigh(state node){
        vector<state> nodes;
        for(int k=0;k<4;k++){
            int nx=node.F+dx[k];
            int ny=node.S+dy[k];
            if(valid(nx,ny)){
                cell[nx][ny]=0;
                nodes.push_back({nx,ny});
            }
        }
        return nodes;
    }

    void bfs(int i,int j){
        queue<state> q;
        q.push({i,j});
        // vis[i][j]=1;
        cell[i][j]=0;

        while(!q.empty()){
            state node = q.front();q.pop();
            // int x=node.F,y=node.S;
            for(auto v:neigh(node)){
                // vis[v.F][v.S]=1;
                cell[v.F][v.S]=0;
                q.push(v);
            }
        }

    }
    int numIslands(vector<vector<char>>& grid) {
        // vector<vector<int>> cell(m,vector<int>(n,0));
        m=grid.size();
        n=grid[0].size();
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                cell[i][j]=grid[i][j]-'0';
                // vis[i][j]=0;
            }
        }
        
        int comp=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(cell[i][j]==1 && !vis[i][j]){
                    comp++;
                    bfs(i,j);
                }
            }
        }
        return comp;

    }
};
26923