def neighbors(matrix, r, c):
live_neighbors = 0
if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r-1][c] != 0:
live_neighbors += 1
if matrix[r-1][c+1] != 0:
live_neighbors += 1
if matrix[r][c-1] != 0:
live_neighbors += 1
if matrix[r][c+1] != 0:
live_neighbors += 1
if matrix[r+1][c-1] != 0:
live_neighbors += 1
if matrix[r+1][c] != 0:
live_neighbors += 1
if matrix[r+1][c+1] != 0:
live_neighbors += 1
return live_neighbors
This is the code I have written so far. How do I count the neighbors of the border cells? If I use this code I would get an index out of range error.