Wrong answer in test cases

i am getting wrong answer in one of the test case and TLE in another one.
here is my code

you are allowed to go up and left as well please read the question carefully.