BFS模版化代码
class Solution {
public:
    int solve(vector<vector<char> >& grid) {
        // write code here
        int dp[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
        int m=grid.size(),n=grid[0].size(),res=0;
        queue<pair<int,int>> q;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]=='1'){
                    q.emplace(i,j);
                    res++;
                    while(q.size()){
                        auto[I,J]=q.front();
                        q.pop();
                        for(int k=0;k<4;k++){
                            int i1=I+dp[k][0],j1=J+dp[k][1];
                            if(i1>-1 && i1<m && j1>-1 && j1<n && grid[i1][j1]=='1'){
                                grid[i1][j1]='2';
                                q.emplace(i1,j1);
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
};