FCTRE WA (DFS approach)

i am receiving WA in FCTRE . I am applying DFS.
Problem link : FCTRE Problem - CodeChef

This is my solution in python : CodeChef: Practical coding for everyone
I am solving this question only for first subtask.
Is the problem because of the recursive code for dfs or anything else?