def scanner():
    def scan1(state):
        ... # as above
        
    counts_by_column = [0] * COLS
    counts_by_row = [0] * ROWS
    state = [0] * N
    latch = [0] * N
    old_state = [0] * N

    while True:
        yield None # at the start of a scan

        scan1(state)
        scan1(latch)
        while state != latch: # something changed
            state, latch = latch, state
            scan1(latch)

        for column in range(COLS):
            if counts_by_column[column] < 2:
                continue
            for row in range(ROWS):
                i = row + column * ROWS
                if not state[i]:
                    continue
                if counts_by_row[row] < 2:
                    continue    
                state[i] = old_state[i]
                
        for i in range(N):
            if state[i] != old_state[i]:
                yield i | (state[i] << 15)

        old_state, state = state, old_state