sum with the same dimensions as the input matrix plus one extra row and column (for easier calculations).sum matrix by iterating over the input matrix and updating sum[i+1][j+1] to be the sum of matrix[i][j] plus the values from the left and above, minus the value diagonally up-left to avoid double-counting.sumRegion by applying the inclusion-exclusion principle to obtain the sum of the desired submatrix using the pre-computed sum matrix.