Finding Sum of Neighbors in 2D or Multidimensional Array (Grid)

Good way of finding the sum of the neighboring values in a 2d array. I need to find the sum of all neighboring values at an index position.

E.g. int[,] array = new int[3, 4] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } };

Here neighbors of 5.

I.e. [1.1] will be [0,0] + [0,1] + [0,2] + [1,0] + [1,2] + [2,0] + [2,1] + [2,2]

And Sum will be 40. 
  1. public class SumOfNeibours   
  2. {  
  3.     int[, ] array = new int[3, 4]   
  4.     {  
  5.         {  
  6.             0, 1, 2, 3  
  7.         }, {  
  8.             4, 5, 6, 7  
  9.         }, {  
  10.             8, 9, 10, 11  
  11.         }  
  12.     };  
  13.     public void GetSumOfNeibours()   
  14.     {  
  15.         int newval = 0;  
  16.         for (int i = 0; i < array.Length; i++)   
  17.         {  
  18.             var x = i / 4;  
  19.             var y = i % 4;  
  20.             var startX = x - 1 < 0 ? 0 : x - 1;  
  21.             var endX = x + 1 > 2 ? 2 : x + 1;  
  22.             var startY = y - 1 < 0 ? 0 : y - 1;  
  23.             var endY = y + 1 > array.GetLength(0) ? array.GetLength(0) : y + 1;  
  24.             //array.GetLength(0) ? array.GetLength(0) : y + 1;  
  25.             for (int z = startX; z <= endX; z++)   
  26.             {  
  27.                 for (int j = startY; j <= endY; j++)   
  28.                 {  
  29.                     if (x == z && y == j)   
  30.                     {  
  31.                         continue;  
  32.                     }  
  33.                     var v = array[z, j];  
  34.                     Console.WriteLine(array[z, j] + " ");  
  35.                     newval += array[z, j];  
  36.                 }  
  37.             }  
  38.             Console.WriteLine("\n ");  
  39.             Console.WriteLine("Sum is" + newval);  
  40.             newval = 0;  
  41.         }  
  42.     }  
  43. }