same code giving runtime error and then wrong answer.

Problem

Code1

Code2

I believe the test cases contain some tests wherin Zi>k.

What is wrong in my approach . I am preprocessing the matrix to efficiently answer submatrix sum in O(1) and then iterating over all possible submatries.
Any help is appreciated.

//