Since all the corners are included twice , they need to be subtract from the sum
sum -= ( A [ i ] [ j ] + A [ i + k ] [ j ] +   \ A [ i ] [ j + k ] + A [ i + k ] [ j + k ] ) NEW_LINE return sum NEW_LINE