Unable to find bug in DFS problem — TopCoder SRM 211 Div1 500 GrafixMask.

Hi there, help needed.
Find the details on the question here. You may answer either here, or on codeforces. Thanks.