title: How to lable connected components in a 2D array?
body: <p>I have 2d array like this: </p>
  
  <p><code>0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
  0 0 1 1 1 0 1 1 0 1 0 0 0 0 0 1
  0 0 1 0 1 0 1 1 0 1 0 1 1 1 0 1
  0 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1
  0 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1
  0 0 1 1 1 0 0 0 0 1 0 1 0 1 0 1
  0 0 0 0 0 1 1 1 0 1 0 1 1 1 0 1
  0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1
  0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 1
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0
  0 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0
  0 1 0 1 1 1 1 1 0 0 1 1 1 1 0 0
  0 1 0 1 0 1 0 1 0 0 1 1 1 1 0 0
  0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
  0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0</code></p>
  
  <p>And I want to label the connected components (4 directions)to be like this: </p>
  
  <p><code>0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
  0 0 2 2 2 0 3 3 0 1 0 0 0 0 0 1
  0 0 2 0 2 0 3 3 0 1 0 4 4 4 0 1
  0 0 2 0 2 0 0 0 0 1 0 4 0 4 0 1
  0 0 2 0 2 0 0 0 0 1 0 4 0 4 0 1
  0 0 2 2 2 0 0 0 0 1 0 4 0 4 0 1
  0 0 0 0 0 5 5 5 0 1 0 4 4 4 0 1
  0 0 0 0 0 5 0 5 0 1 0 0 0 0 0 1
  0 0 0 0 0 5 5 5 0 1 1 1 1 1 1 1
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 6 6 6 6 6 0 0 0 0 7 7 7 7 0 0
  0 6 0 0 0 6 0 0 0 0 7 0 0 7 0 0
  0 6 0 6 6 6 6 6 0 0 7 7 7 7 0 0
  0 6 0 6 0 6 0 6 0 0 7 7 7 7 0 0
  0 6 6 6 6 6 6 6 0 0 0 0 0 0 0 0
  0 6 6 6 6 6 6 6 0 0 0 0 0 0 0 0</code></p>
  
  <p>Please help me solve this problem. Thanks!!!(java or javascript code example would be great) </p>