Find group of contagious cells in a 3D matrix.
===

I want to find groups of contagious cells in a matrix. 

So for example let’s consider a 2D matrix below.  
  http://i.stack.imgur.com/TuzUk.png


So in the given matrix there are 2 contagious groups of cells having value “1” in it. As shown in the figure below. 

 
   http://i.stack.imgur.com/oNcol.png


One way to find these groups is,

a.	Assign 1st cell with value 1 a different value let’s say “A”. Then examine cells with value 1 which are adjacent to “A” and set the value in these cells as “A”.  Search this until no more contagious cells are found. 

b.	In the next step increment “A” to “B” and start with a cell having value “1”. And follow the same step as above. 
But this is kind of brute force and it won’t be efficient in 3D.

Does anyone know any algorithm which can be used by little tweaking or something?
 
Or any easy solution to this problem? 

Thanks for your help in advance. Please let me know if require more information. 

Thanks. 


NOTE: Sorry, I am a new user so not allowed to post images so have typed in the link to that images. 
