Sum of all submatrix from given matrix approach 1 video

In this particular approach, we are supposed to extract all possible submatrices and then find the sum but how he is extracting submatrices from the given matrix is not clear to me. Can anyone help?

for constructing a matrix we need 4 points
cordinates of starting point (top left => si,ei ) and
coordinates of end point (bottom right => sj ej)
to get this we need 4 loops
and after that to sum the given matrix we need 2 more loops
the code of above approach is attached below

int sumofAllSubmatrix1(int arr[][50],int n,int m){
    int ans=0;
    for(int si=0;si<n;si++){
        for(int sj=0;sj<m;sj++){
            for(int ei=si;ei<n;ei++){
                for(int ej=sj;ej<m;ej++){

                    // NOW CALCULATE SUM OF SUBMATRIX (si,sj) to (ei,ej)
                    int sum=0;
                    for(int i=si;i<=ei;i++){
                        for(int j=sj;j<=ej;j++){
                            sum+=arr[i][j];
                        }
                    }
                    ans+=sum;
                }
            }
        }
    }
    return ans;
}

i hope this help
if you have doubts feel free to ask

Yes that helps. Thanks.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

plz give feedback it is important
thanks