title: How to get the neighbours of a cell in a matrix in C#
body: <p>I tried to print the neighbours of any given cell by using its row index and column index in a 2D array using the following method: </p>
  
  <pre><code>public static List&lt;int[,]&gt; getAUVNieghborsCells(int [,] grid, int row, int col)
  {
  
      List&lt;int[,]&gt; n = new List&lt;int[,]&gt;();
  
      //define boundaries
      int rowLen = Math.Min(row + 1, grid.GetLength(0) - 1),
          colLen = Math.Min(col + 1, grid.GetLength(1) - 1),
          rowIdx = Math.Max(0, row - 1),
          colIdx = Math.Max(0, col - 1);
  
      for (int i = rowIdx; i &lt;= rowLen; i++)
      {
          for (int j = colIdx; j &lt;= colLen; j++)
          {
              //if it is our given index, continue
              if (i == row &amp;&amp; j == col)
                  continue;
  
              int[,] temp = new int[1, 2];
  
              temp[0, 0] = i;
              temp[0, 1] = j;
  
              n.Add(temp);
          }
      }
  
    return n;
  }
  </code></pre>
  
  <p>I send (4,0) but I got:</p>
  
  <blockquote>
    <p>|(4,0)||(4,1)||(5,1)||(6,0)||(6,1)| </p>
  </blockquote>
  
  <p>which are wrong as they are the neighbors for (5,0) and (4,0). Where is the problem?</p>