title: Program counting adjacent elements in a 2D array gives inconsistent results
body: <p>I have a piece of program that i have to fill. It is supposed to count adjacent numbers in a 5 by 5 binary matrix. For example a matrix like this:</p>
  
  <pre><code>0 1 1 1 0 
  1 0 0 0 1 
  1 0 0 0 0 
  1 0 0 1 0 
  0 1 0 1 0 
  </code></pre>
  
  <p>Should return 8, you can only move horizontally or vertically.</p>
  
  <p>Here is the code that generates these said matrices and it can't have any modifications after i'm done.</p>
  
  <pre><code>import java.util.Random;
  
  public class Test {
  
  public static void main(String[] args) {
      final Random r = new Random();
  
  
      for (int kerrat = 0; kerrat &lt; 10; kerrat++) {
          int[][] alkioTaulukko = new int[5][5];
  
          System.out.println("Matriisin");
          for (int i = 0; i &lt; alkioTaulukko.length; i++) {
              System.out.print("");
              for (int j = 0; j &lt; alkioTaulukko[i].length; j++) {
                  alkioTaulukko[i][j] = r.nextInt(2);
                  System.out.print("" + alkioTaulukko[i][j] + " ");
              }
              System.out.println("");
          }
  
          System.out.print("suurimman yhtenäisen alueen koko on ");
          System.out.println(laskeSuurinAlue(alkioTaulukko));
          System.out.println("");
      }
  
  }
  
  }
  </code></pre>
  
  <p>And finally here is my solution to the problem.</p>
  
  <pre><code>static int  laskeSuurinAlue(int[][] matriisi) {
  
      int vierekkaiset = 0;
  
      int rivi = matriisi.length;
      int palkki = matriisi[0].length;
  
      for (int r = 0; r &lt; rivi; r++)
      {
          for (int p = 0; p &lt; palkki; p++)
          {
              if ((p+1 &lt; palkki) &amp;&amp; (matriisi[r][p] == matriisi[r][p+1]))//loops through the rows
                  vierekkaiset++;
  
              if ((r+1 &lt; rivi) &amp;&amp; (matriisi[r][p] == matriisi[r+1][p]))//loops through the columns
                  vierekkaiset++;
          }
      }
      return vierekkaiset;
  }
  </code></pre>
  
  <p>What happens is that my solution always brings up too big results and i'm failing to see any pattern between each run. However if i use a smaller matrix like this:</p>
  
  <pre><code>int[][] array = {{1, 0, 1}, {0, 1, 1}, {0, 1, 0}};
  </code></pre>
  
  <p>The result is correctly 4.</p>
  
  <p>And if i use a bigger one like this:</p>
  
  <pre><code>int[][] arr = {{1,0,1,1,0},
              {0,0,1,0,0},
              {0,0,1,0,1},
              {1,1,1,0,1},
              {0,0,1,0,0}
      };
  </code></pre>
  
  <p>The result is always 20.</p>
  
  <p>Finally here is my code at its current state:</p>
  
  <pre><code>    import java.util.Random;
  
      import static java.util.Arrays.deepToString;
  
      public class Test {
      public static void main(String[] args) {
      final Random r = new Random();
      int[][] array = {{1, 0, 1}, {0, 1, 1}, {0, 1, 0}};
      int[][] arr = {{1,0,1,1,0},
              {0,0,1,0,0},
              {0,0,1,0,1},
              {1,1,1,0,1},
              {0,0,1,0,0}
      };
      for (int kerrat = 0; kerrat &lt; 10; kerrat++) {
          int[][] alkioTaulukko = new int[5][5];
  
          System.out.println("Matriisin");
          for (int i = 0; i &lt; alkioTaulukko.length; i++) {
              System.out.print("");
              for (int j = 0; j &lt; alkioTaulukko[i].length; j++) {
                  alkioTaulukko[i][j] = r.nextInt(2);
                  System.out.print("" + alkioTaulukko[i][j] + " ");
              }
              System.out.println("");
          }
  
          System.out.print("suurimman yhtenäisen alueen koko on ");
          System.out.println(laskeSuurinAlue(arr));//Change to arr,array or alkioTaulukko to run the code with different matrices
          System.out.println(deepToString(arr));
          System.out.println("");
  
      }
  
  }
  
  
  static int  laskeSuurinAlue(int[][] array) {
  
  
      int counter = 0;
  
      int rowLimit = array.length;
      int colLimit = array[0].length;
  
      for (int r = 0; r &lt; rowLimit; r++)
      {
          for (int c = 0; c &lt; colLimit; c++)
          {
              if ((c+1 &lt; colLimit) &amp;&amp; (array[r][c] == array[r][c+1]))
                  counter++;
  
              if ((r+1 &lt; rowLimit) &amp;&amp; (array[r][c] == array[r+1][c]))
                  counter++;
          }
      }
      return counter;
  }
  
  }
  </code></pre>