Matrix queries (passed one case but TLE in other two)

i don’t know why am getting tle
please help me
my code: https://ide.codingblocks.com/s/107665

question link
https://hack.codingblocks.com/contests/c/668/813