But what will happen if i=0 and j=0

as to calculate the sum of submatrix we need to perform
sum = pre [ bi ] [ bj ] - pre [ i-1 ] [ bj ] - pre [ bi ] [ j-1 ] + pre [ i -1 ] [ j-1 ] but what if i=0 and j=0. there will be a garbage value since pre[-1][bj] and pre[bi][-1] are not defined.

Hey!, first of all this won’t give garbage value,instead it will give segmentation error as [-1] index doesn’t exist in C++. Now for your query — you can handle this by starting indexing from 1 instead of 0 and set pre[ all_rows ][0] and pre[0][ all_columns ] to 0.

Here is the code: and the output is also attached and there is no segmentation error.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[4][4];
int val=1;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
a[i][j]=val;
val++;
}
int csum[4][4]={0};//prefix array row wise
for(int i=1;i<4;i++)
for(int j=0;j<4;j++)
a[i][j]+=a[i-1][j];

for(int i=0;i<4;i++)//prefix array column wise
    for(int j=1;j<4;j++)
        a[i][j]+=a[i][j-1];

int n=4;
int sum=0;
for(int li=0;li<n;li++)//top left i
    for(int lj=0;lj<n;lj++)//top left j
        for(int bi=li;bi<n;bi++)// bottom right i
            for(int bj=lj;bj<n;bj++)//bottom right j
              {
				  sum+=a[bi][bj]-a[li-1][bj]-a[bi][lj-1]+a[li-1][lj-1];
				  if(li==0&&lj==0)
				  	cout<<"a["<<li-1<<"]["<<bj<<"]:"<<a[li-1][bj]<<"\ta["<<bi<<"]["<<lj-1<<"]:"<<a[bi][lj-1]<<endl;
				}
cout<<sum;

}

It’s alright if you are not getting segmentation fault (this is because a[-1] refers to 4 bytes earlier to that allocated to a[0], but online judges tend to give SEG FAULT), still it’s not a good practice.
I think your query was already answered that how to avoid it!