I understand that you want to find all connected components in a binary image.
I suggest you use a sequential 2 pass algorithm to find the connected components. This algorithm is in the link posted below. If you have issues with your code, please post it