Gold Grid hackerblock problem

question link: https://hack.codingblocks.com/app/contests/1338/793/problem.

I am getting a MEMORY LIMIT EXCEEDED in 3 test cases while 2 test cases are passed successfully.

my code link: http://p.ip.fi/Brch