int n = grid.size(); //num_rows
int m = grid[0].size(); //num_cols
int sum = 0,i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m1;j++)
{
sum += abs(grid[i][j+1]grid[i][j]); //find vertical edges
}
sum = sum+grid[i][j]+grid[i][0]; //account for 2 corner blocks
}
for(j=0;j<m;j++)
{
for(i=0;i<n1;i++)
{
sum += abs(grid[i+1][j]grid[i][j]); //find horizontal edges
}
sum = sum + grid[i][j] + grid[0][j]; //account for 2 corner blocks
}
return sum;
C++  Easy to Understand (Find edges)


Your runtime ?
Mine isint ans=0; int nc=grid[0].size(),nr=grid.size(); for(int i=0;i<nr;i++) for(int j=0;j<nc;j++) { if(grid[i][j]==1) { ans+=4; if(i1>=0) ans=grid[i1][j]; if(j+1<=nc1) ans=grid[i][j+1]; if(i+1<=nr1) ans=grid[i+1][j]; if(j1>=0) ans=grid[i][j1]; } } return ans; }
It's runtime is 129ms could you suggest some ways to reduce it.