title: connected neighbours in a java 2d array
body: <p>I'm designing an engine for a game which has a 2D array like this:</p>
  
  <pre><code>0,1,1,2,0
  0,1,2,1,1
  1,0,1,0,2
  2,1,2,0,0
  2,0,1,0,0
  </code></pre>
  
  <p>I'm stuck at the "game over" condition as it has to check if the 1's or 2's are connected.
  It should declare the player with 1's as winner and return this:</p>
  
  <pre><code> 1 1
   1   1 1
  1  1
  1
    1
      1
  </code></pre>
  
  <p>I've tried using recursion by checking every position in the array and checking its neighbors in all 8 directions but the method took 45 seconds to run which is inefficient.</p>
  
  <p>Does anyone have any ideas?  A pseudo-code example would be appreciated (I'm a slow learner).</p>