Test cases not passed in piyush and magical park problem

i have tried this problem but getting wrong answer in some test cases. can u tell me how to modify it to pass all the test cases ?

made updates in the code

u werent
checking for the corner cases at the correct position
check this code