Doubt in Counting in Byteland

Problem link:
https://online.codingblocks.com/app/player/115374/content/73625/5176/code-challenge

Code Link:

Can you please help in finding the error. First two test cases are working correctly, but shows error for others.

@mgfags

  1. For wrong ans declare your count1 as long long int. Val can be upto 10^9 and their sum might not get stored in int.
  2. For runtime error increase size of your matrix to matrix[103][103][103]. See in update() and calculate_sum() function, you have run while loop till z<=102, y<=102 and z<=102 but 102 index doesn’t exist.
    Here is the updated code https://ide.codingblocks.com/s/278049