How to find sum of neighbor values in 2d array of any index

how to find sum of neighbor values in 2d array of any index`
7 0 4
0 8 3
12 6 0
ex for 8 it is (7+4+3+6+12)

simply
a(i-1)(j-1) + a(i)(j-1) + a(i-1)(j) + a(i+1)(j+1) + a(i)(j+1) + a(i+1)(j)
i hope its cleared if yes dont forget to mark it as resolved @guptanikhil898 :smiley:

i am not asking about only mid one i want for all element it cab be at ends so i want ageneral one it will not work for index like i=0 j=0

@guptanikhil898 just check if index exists or not like when you are calculating i-1 , j check if i-1 greater than eq 0 then only take it in sum

this way for 0,0 automatically 0 1 , 1 0 , 1 1 will be checked others will be discarded

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.