Why does my approach give Memory Limited Exceeded?


#1
void f1(vector<string> &A, int i, int j, int count)
{
    A[i][j] = count - '0';
    if(i-1 >= 0 && A[i-1][j] == 'X' )
        f1(A, i-1, j, count);
    if(i+1 < A.size() && A[i+1][j] == 'X' )
        f1(A, i+1, j, count);
    if(j+1 < A[i].size() && A[i][j+1] == 'X' )
        f1(A, i, j+1, count);
    if(j-1 >= 0 && A[i][j-1] == 'X' )
        f1(A, i, j-1, count);
    return;
}
int Solution::black(vector<string> &A) 
{
    int count = 0;
    for(int i = 0 ; i < A.size() ; i++)
    {
        for(int j = 0 ; j < A[i].size() ; j++)
        {
            if(A[i][j] == 'X' )
            {
                //increment black shape
                count++;
                f1(A, i, j, count);
            }
            
        }
    }
    return count;
}

#2

Use A[i][j] = ‘0’; instead A[i][j] = count - ‘0’;