How not to go beyond the C#
Let's say there's a two-bed 5x5. The central element [3,3] and each element of the surrounding element (i.e. [2,2] [2,3] ... [4.4]) shall be variable. That's what we do in these elements, the meaning of which we changed, etc. How do you plan to prevent a crossroads?
The checking of the indices in the double range can be performed as follows:
int[,] array = new int[5, 7];
var w = array.GetLength(0); // вернет 5
var h = array.GetLength(1); // вернет 7
if (x >= 0 && x < w && y >= 0 && y < h)
// array[x, y]