Google kickstart round G

can anybody why i got WA in pretest 2
problem:https://codingcompetitions.withgoogle.com/kickstart/round/00000000001a0069/0000000000414a23
my solution:https://pastebin.com/TBuFMrBd

in the condition of while loop you kept !visited[x][y] && x<n && y<n
if x or y go out of bounds , it is not checked .
try changing condition to x<n && y<n && !visited[x][y]

Still getting WA