I am getting segmentation fault

int sumofsubmatrix(int **arr,int n, int m, int tli, int tlj, int bri, int brj){
int sum = 0;
for(int li = 0; li <= n-1; li++){
for(int lj = 0;lj <= m-1;lj++){
for(int bi = li; bi <= n-1;bi++){
for(int bj = lj; bj <= m-1;bj++){
sum += (arr[bi][bj] - arr[li-1][bj] - arr[bi][lj-1] - arr[li-1][lj-1]);
}
}
}
}
return sum;
}

int main()
{
int n, m;
cin >> n >> m;
int *arr = new int[n];
for(int i = 0;i < n;i++){
arr[i] = new int[n];
for(int j = 0;j < m;j++){
cin >> arr[i][j];
}
}
int tli, tlj, bri,brj;
cin >> tli >> tlj >> bri >> brj;
cout << sumofsubmatrix(arr, n, m, tli,tlj, bri,brj) << endl;
return 0;
}

what is wrong in this

hello @ayushi1150

here for li=0 ,li-1 will be -1 which is wrong , as we cannot acces negative index.

so you need to handle the following case separately.
li=0 ,lj>0
li>0 ,lj=0
li=0 , lj=0
li>0 , lj=0