SOS problem in starters 21.

https://www.codechef.com/submit/SOS
I am getting time limit exceeded in just one task i.e task 2. Not able to understand why. Please help me to get the mistake which I am making.