def is_xmas(lines: list[str], row_idx: int, col_idx: int) -> bool: if lines[row_idx][col_idx] != 'A': return False if row_idx == 0 or col_idx == 0 or row_idx == len(lines) - 1 or col_idx == len(lines[0]) - 1: return False found_mas_count = 0 for row_step in range(-1, 2, 2): for column_step in range(-1, 2, 2): current_corner = lines[row_idx + row_step][col_idx + column_step] if current_corner == 'M': if lines[row_idx - row_step][col_idx - column_step] == 'S': found_mas_count += 1 else: return False elif current_corner != 'S': return False return found_mas_count == 2 data = [] with open("input") as f: for line in f: data.append(line.strip()) result = 0 for row, line in enumerate(data): for col in range(len(line)): if is_xmas(data, row, col): result += 1 print(result)