Building the 2D prefix array also uses inclusion-exclusion: You add the current cell, plus the rectangle above, plus the rectangle to the left.
But the top-left rectangle gets counted twice, so subtract it once. Initialize and for all . This handles edge cases cleanly.