SUM OF ALL SUBMATRIX

int main()
{
int n,m;
int presum=0;
cin>>n>>m;
int a[100][100]={0};
int pre[100][100]={0};

// input the array and adds the elements in rows
for(int i=0;i<n;i++)
{
    for(int j=0;j<m;j++)
    {
        cin>>a[i][j];
        presum+=a[i][j];
        pre[i][j]=presum;
    }
    presum=0;
}

// adds the elements  in columns
for(int j=0;j<m;j++)
{
    for(int i=0;i<n;i++)
    {
        presum+=pre[i][j];
        pre[i][j]=presum;
    }
    presum=0;
}

cout<<endl;

int sum=0;

for(int ti=0;ti<n;ti++)
{
    for(int tj=0;tj<m;tj++)
    {
        for(int bi=ti;bi<n;bi++)
        {
            for(int bj=tj;bj<m;bj++)
            {
                sum+=pre[bi][bj]-pre[ti-1][bj]-pre[bi][tj-1]+pre[ti-1][tj-1];
            }
        }
    }
}

cout<<sum;
return 0;

}

Can you check for the mistake.The only correct answer it gives are for a 2x2 arrray.

hi @anasmir24
i have check your code
it is giving correct ans

for which input it is giving wrong ans??

i checked the same code in coding blocks ide.It shows the correct answer but for some reason in vs code it shows wrong answer.Like for e.g in a 3x3 matrix with all elemnts 1 the result it gives is 1251484712

okay then consider ans of online ide as correct

because this may be your compiler error

if your doubt is resolved Please mark it as resolved