4WALK Problem

I got the information that my program exceeded time limit but i don’t really know why.
https://www.codechef.com/viewsolution/30976973