//below is the code
#include
using namespace std;
int main()
{
int m,n,a[10][10], sum =0;
cout<<"Enter the number of rows and columns of the array "<<endl;
cin>>m>>n;
cout<<"Enter the array elements "<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"the prefix sum array is "<<endl;
//for row wise prefix
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(j!=0)
{
a[i][j] = a[i][j] + a[i][j-1];
}
}
}
//for column type prefix
for(int i=0; i<n; i++)
{
for(int j=0;j<m;j++)
{
if(j!=0)
{
a[j][i] = a[j][i] + a[j-1][i];
}
}
}
//Printing the prefix sum array
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
for(int li=0;li<m;li++)
{
for(int lj=0;lj<n;lj++)
{
for(int bi=li;bi<m;bi++)
{
for(int bj=lj;bj<n;bj++)
{
sum += a[bi][bj] -a[(((li-1)==-1)?0:li-1)][bj] - a[bi][(((lj-1)==-1)?0:lj-1)] + a[(((li-1)==-1)?0:li-1)][(((lj-1)==-1)?0:lj-1)];
//li and lj at 0 will give li-1==-1 so i used ternary operator to prevent the same but sum obtained is wrong
}
}
}
}
cout<<sum;
return 0;
}