title: Recursive Flood Fill - Checking Boundries
body: <p>I Have a recursive flood fill of legal neighbours in matrix ( legal neighbour is a neighbour with same color), the flood is not filling all the legal neighbours in the array.
  the board I`m using for testing is :
  <br></p>
  
  <pre><code>int[][] map={{4,0,0,0},
               {0,4,0,0},
               {0,4,0,0},
               {0,4,0,0}};
  
     fill(map,1,1,9,4);// calling to function.
  </code></pre>
  
  <p>the output is :
  <br></p>
  
  <pre><code>4000
  0900
  0900
  0900
  </code></pre>
  
  <p><strong>Edit</strong>
  If i`m changing the map to: </p>
  
  <pre><code>int[][] map={{4,0,0,0},
           {4,4,0,0},
           {0,4,0,0},
           {0,4,0,0}};
  </code></pre>
  
  <p>The output will be:<br></p>
  
  <pre><code>4000
  4900
  0900
  0900
  </code></pre>
  
  <p>The two left 4 number need to be filled too.
  and my recursive function is :
  <br></p>
  
  <pre><code>public static void fill(int[][] map, int row, int col, int color,int oldColor) 
  
     {
  
  System.out.println("row is: "+row+"col is:"+col);
  if ((row &lt;= 0) || (row &gt;= map.length) || (col &lt;= 0) || (col &gt;= map.length) ) return; 
  
  if(map[row][col]==color)
          return;
  
  if(map[row][col]==oldColor)
      {
          map[row][col]=color;
      }
  if(col+1&lt;=map.length)
        fill(map, col+1, row,color,oldColor);
   if((col-1)&lt;=0) 
        fill(map,col-1, row,color,oldColor);
  
    if(row+1&lt;=map.length)
        fill(map, col, row+1,color,oldColor);
    if((row-1)&lt;=0)
        fill(map, col, row-1,color,oldColor);
  
     } 
  </code></pre>
  
  <p><strong>Changing the code</strong><br></p>
  
  <pre><code>public static void fill(int[][] map, int row, int col, int color,int oldColor) {
      System.out.println("row is: "+row+"col is:"+col);
  if ((row &lt; 0) || (row &gt; map.length) || (col &lt; 0) || (col &gt; map.length) || map[row]                        [col]!=oldColor ) return; 
  
  if(map[row][col]==color)
          return;
  
  if(map[row][col]==oldColor)
      {
          map[row][col]=color;
      }
  
  fill(map, col, row-1,color,oldColor);
  fill(map, col+1, row,color,oldColor);
      fill(map, col, row+1,color,oldColor);
      fill(map,col-1, row,color,oldColor);
    }
  </code></pre>
  
  <p>The output is now:<br></p>
  
  <pre><code>9000
  9900
  0900
  0400
  </code></pre>