So finden Sie die Nachbarn eines Elements in Matrix Python

mat = "NxN matrix"
a, b = (2, 3) # the index of the element 
neighbors = [mat[i][j] for i in range(a-1, a+2) for j in range(b-1, b+2) if i > -1 and j > -1 and j < len(mat[0]) and i < len(mat)]
ProCoderMove