import itertools

# All possible binary strings of length 5
POSSIBLE_PATTERNS = [''.join(x) for x in itertools.product('01', repeat=5)]

def flip(c):
    return '1' if c == '0' else '0'

def apply_pattern_to_row(pattern, row):
    new_row = row[:]
    for c in range(5):
        if pattern[c] == '1':
            for cc in range(c-1, c+2):
                if 0 <= cc < 5:
                    new_row = new_row[0:cc] + flip(new_row[cc]) + new_row[cc+1:]
    return new_row

print('Enter the grid as five lines of five characters each (no spaces within a line).\n0 is off, 1 is on.')
grid = [input() for _ in range(5)] + ['00000'] # add dummy extra row for convenience
best_solution = None
best_count = 25
for p in POSSIBLE_PATTERNS:
    curr_pattern = p
    solution = []
    curr_row = grid[0]
    for i in range(5):
        next_pattern = apply_pattern_to_row(curr_pattern, curr_row)
        next_row = ''
        for j in range(5):
            next_row += flip(grid[i+1][j]) if curr_pattern[j] == '1' else grid[i+1][j]
        curr_row = next_row
        solution.append(curr_pattern)
        curr_pattern = next_pattern
    if next_pattern == '00000': # we don't care about next_row now since it's off the board
        press_count = sum([r.count('1') for r in solution])
        print(solution)
        if press_count < best_count:
            best_count = press_count
            best_solution = solution
print('No solution' if not best_solution else 'Solvable! An optimal solution is to press the following:\n' + '\n'.join(best_solution))

