DFS approach using C++, can you optimize it!


#1
void markVisited(vector<string> &A,int i,int j,int row,int col)
{
    if(i<0||i>=row||j<0||j>=col||A[i][j]!='X')
        return;
    A[i][j] = 'O';
    
    markVisited(A,i-1,j,row,col);//up
    markVisited(A,i+1,j,row,col);//down
    markVisited(A,i,j-1,row,col);//left
    markVisited(A,i,j+1,row,col);//right
}

int Solution::black(vector<string> &A) 
{
    int row = A.size();
    if(row == 0)
        return 0;
    int col = A[0].length();
    int count = 0;
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            if(A[i][j] == 'X')
            {
                markVisited(A,i,j,row,col);
                count++;
            }
        }
    }
    return count;
}

#2

crisp,short and very understandable code.THANK YOU FOR THIS.