Codeforces Connected Components Cycles Grid Problem (Fox and Two Dots)

Problem Link: Problem - B - Codeforces
Submission Link: Submission #83349621 - Codeforces

I am trying to detect a cycle by using dfs and trying not to print Yes if we have come back to the last visited node in our dfs search… I can’t understand where I am going wrong. It’s failing some tests. Please help.

Probably if (!vis[n][m]) { is not what you meant to write

Thank you so much, I have no idea how I missed that