from collections import Counter
def find_common_element(matrix):
rows = len(matrix)
if rows == 0:
return -1
counter = Counter()
for row in matrix:
unique = {row[0]}
unique.update(val for i, val in enumerate(row[1:], start=1) if val != row[i - 1])
counter.update(unique)
return next((num for num, count in counter.items() if count == rows), -1)
matrix = [
[1, 2, 3, 5, 36],
[4, 5, 7, 9, 10],
[5, 6, 8, 9, 18],
[1, 3, 5, 8, 24]
]
result = find_common_element(matrix)
print(f"Common element in all rows: {result}" if result != -1 else "No common element found in all rows.")
'''
run:
Common element in all rows: 5
'''