Vertical order print TLE

I am getting TLE in test case 1
plzz check my code what changes should I make in my code