new_contents: def count_neighbours( grid , row , col ) :
      
      left = max( 0 , row - 1 )
      right = min( row + 2 , len( grid ) )
      top = max( 0 , col - 1 )
      bottom = min( col + 2 , len( grid[0] ) )
      
      return sum( grid[i][j] for i in range( left , right ) for j in range( top , bottom ) if i != row or j != col )
  
  
  if __name__ == '__main__':
      #These "asserts" using only for self-checking and not necessary for auto-testing
      assert count_neighbours(((1, 0, 0, 1, 0),
                               (0, 1, 0, 0, 0),
                               (0, 0, 1, 0, 1),
                               (1, 0, 0, 0, 0),
                               (0, 0, 1, 0, 0),), 1, 2) == 3, "1st example"
      assert count_neighbours(((1, 0, 0, 1, 0),
                               (0, 1, 0, 0, 0),
                               (0, 0, 1, 0, 1),
                               (1, 0, 0, 0, 0),
                               (0, 0, 1, 0, 0),), 0, 0) == 1, "2nd example"
      assert count_neighbours(((1, 1, 1),
                               (1, 1, 1),
                               (1, 1, 1),), 0, 2) == 3, "Dense corner"
      assert count_neighbours(((0, 0, 0),
                               (0, 1, 0),
                               (0, 0, 0),), 1, 1) == 0, "Single"
  
subject: Create count-neighbours.py