Why is my code giving WA on last test case?

Please check my code and tell my why is it giving WA on last test case

Link: https://ide.codingblocks.com/s/259798

@HemantKumar
You have not red question properly. You can move to any of 8 adjacent characters. Consider this example
2
a abcd
2
ac
db
here output should be “a abcd”. we can make “abcd” by moving in this way. { (0,0) -> (1,1) -> (0,1) -> (1,0) }.