• I no have idea how to avoid IndexError

Question related to mission Moore Neighbourhood

 
row = 4 # stroka
col = 4 # stolbec
grid = ((1, 0, 0, 1, 0),
        (0, 1, 0, 0, 0),
        (0, 0, 1, 0, 1),
        (1, 0, 0, 1, 0),
        (0, 0, 1, 0, 0))
asd = []
n = -1
while n < 2:
    if n == -1 or n == 1:
        asd.append(grid[row+n][col+n])  # main diagonal
        asd.append(grid[row][col+n])    # horizon
        asd.append(grid[row+n][col])    # vertical
        asd.append(grid[row+n][col-n])  # secondary diagonal
    n += 1
print(asd.count(1))
10