why not giving correct ans in every case in mike and matrices(MIKE1)

#include<stdio.h>
typedef long long int ll;
int main()
{ int i,j,I,J,n,m;
ll e1=0,e2=0,L;
scanf("%d%d",&n,&m);
ll A[n+1][m+1];
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
scanf("%lld",&A[i][j]);
}
scanf("%lld",&L);
for(i=1;i<=L;i++)
{
scanf("%d%d",&I,&J);
if(e1!=-1)
{
if(I>n || j>m)
e1=-1;
else
e1+=A[I][J];
}
if(e2!=-1)
{
if(J>n || I>m)
e2=-1;
else
e2+=A[J][I];
}

}
if(e1>=e2)
printf("%lld",e1);
else
printf("%lld",e2);
return 0;

Please present your code in a proper and understandable way.