'How to count amount of related components on an adjacency matrix

I have to create a method that returns the amount of groups in the graph but i dont find the way to do it,i just need a guide of how i should do it,thanks you. Graph example



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source