def neighbors(row, col, matrix):
    """returns all neighbors of the cell as a list"""
    
    n = len(matrix)
    m = len(matrix[0])
    return [matrix[r][c] for r in range(max(row - 1, 0), min(n, row + 2)) for c in range(max(col - 1, 0), min(m, col + 2)) if r != row or c != col]