title: Algorithm to count the number of connected elements for each element in a matrix
body: <p>I am looking for way to find the number of connected (neighboring) elements in a matrix. 
  I'm given a 2D-array of objects where each object may have a flag set. If the flag is set I need to count the number of neighbours that have the flag set aswell. For each neighbour the process is repeated.</p>
  
  <p>See the image for a visualization of the problem:
  <img src="https://i.stack.imgur.com/N1oHb.png" alt="visualization"></p>
  
  <p>I guess this is a rather common problem. What is it's name so I can do my own research?</p>