Few Lines of code c++


#1
void dfs(int i,int j,int m,int n,vector<string>&A){
    if(i<0||i>=m||j<0||j>=n||A[i][j]!='X')
        return ;
    A[i][j]='Y';
    dfs(i+1,j,m,n,A); //bottom
    dfs(i,j+1,m,n,A);//right
    dfs(i-1,j,m,n,A);//up
    dfs(i,j-1,m,n,A);//left
}


int Solution::black(vector<string> &A) {
    int m=A.size(),n=A[0].length(),cnt=0;
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(A[i][j]=='X'){
                dfs(i,j,m,n,A);
                cnt++;
            }
        }
    }
    return cnt;
}