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

Problem Link: https://codeforces.com/contest/510/problem/B
Submission Link: https://codeforces.com/contest/510/submission/83349621

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