How to find the neighbors of an element (8 neighbors - vertically, horizontally, diagonally) in a two-dimensional array, provided that neighbors can be searched through the border of the array?
There is a two-dimensional array, such as
To find the neighbors of an element inside an array, everything is simple. We combine the coordinates of the current element with the unit and between each other data[x +/- 1][y +/- 1] and get this picture
For the element on the border of the array, the neighbors are 
For an item in the corner
Total we have 1 general case when the element lies inside the array and 8 private ones (4 on the borders, 4 on the corners)
Of course, you can use the case-operator, go through these cases of searching for neighbors and solve this problem head-on, but are there more elegant solutions?


