Preprocessing to calculate sum from i to j
for i in range ( N ) : NEW_LINE INDENT for j in range ( len ( matrix [ i ] ) ) : NEW_LINE INDENT if j > 0 : NEW_LINE INDENT matrix [ i ] [ j ] = matrix [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT return matrix NEW_LINE