Waterloo ACM Programming Contest, September 27, 2008 - Dominos

This is the link of a problem Dominos asked in Waterloo ACM Programming contest on Kattis. I don’t understand why am I getting wrong answer. As much as I understand the problem can be solved by counting the number of maximal connected components but it gives WA. I have used DFS to count the number of connected components.

The link to my code link. Will really appreciate if someone can help.