title: algorithm for largest island in a given matrix
body: <p>Given a 2 X 2 matrix, return different island sizes that is possible</p>
  
  <p>For example, the following matrix should return <code>[5, 7]</code>.</p>
  
  <pre><code>  1 0 0 0 1
    1 1 1 1 1
    0 0 0 0 0
    1 1 1 1 1
  </code></pre>
  
  <p>This is fairly straightforward problem. I am using a boolean visited matrix of same size and traverse the matrix in DFS fashion. I have implemented it here. for some reason, I am getting output as <code>[1]</code>. I tried debugging but my mind stopped working now. I am missing something silly I believe.</p>
  
  <pre><code>public class IslandConnectedCell {
  
      public static void main(String[] args) {
  
          int[][] input = {
                  {1,0,0,0,1},
                  {1,1,1,1,1},
                  {0,0,0,0,0},
                  {1,1,0,1,1}
          };
  
          dfsIsland(input);
  
      }
  
  
      public static void dfsIsland(int[][] input) {
          int rows = input.length;
          int cols = input[0].length;
          List&lt;Integer&gt; countList = new ArrayList&lt;&gt;();
  
          boolean visited[][] = new boolean[rows][cols];
  
          for (int row = 0; row &lt; rows; row++) {
              for (int col = 0; col &lt; cols; cols++) {
                  if (input[row][col] == 1 &amp;&amp; !visited[row][col]) {
                      int count = mark(row, col, input, visited, rows, cols, 0);
                      countList.add(count);
                  }
              }
          }
          System.out.println(countList);
  
      }
  
      public static int mark(int row, int col, int[][] input, boolean[][] visited, int rows, int cols, int count) {
  
          if (row &gt;= rows || row &lt; 0 || col &gt;= cols || col &lt; 0) {
              return 0;
          }
  
          if (input[row][col] == 0 || visited[row][col]) {
              return 0;
          }
  
          visited[row][col] = true;
          count+=1;
  
          for (int i = row - 1; i &lt;= row + 1; i++) {
              for (int j = col - 1; j &lt;= col + 1; j++) {
                  if (i != row || j != col) {
                      mark(i, j, input, visited, rows, cols, count);
                  }
              }
          }
          return count;
      }
  
  }
  </code></pre>