problems / 121

Lights Out on Bob's Panel

medium bitmask 2000 ms · 256 MB

Lights Out on Bob's Panel

Bob rigged a panel of lamps above Alice's desk as a prank, and right now some of them are lit. Next to the panel is a row of switches: each switch is wired to a fixed set of lamps, and pressing it flips the on/off state of exactly those lamps, nothing else. Pressing the same switch twice undoes itself, so Alice will press each switch at most once.

Alice wants every lamp off, using as few switch presses as possible. Bob, being Bob, will demand she read out exactly which switches she used — so among all plans that use the fewest presses, Alice wants the one whose sorted list of switch numbers is lexicographically smallest (compare the two sorted lists term by term; the first list with the smaller entry at the first position where they differ wins).

Formally: there are m lamps, numbered 1 to m, each currently on or off, and n switches, numbered 1 to n. Switch i toggles a fixed, known subset of the lamps. Find a subset of switches, each used at most once, such that pressing all of them (in any order — toggling is commutative) leaves every lamp off, using the fewest switches possible. Among all subsets achieving that minimum, output the lexicographically smallest one, or -1 if no subset of switches can turn every lamp off.

Input

The first line contains two integers m and n (1 ≤ m ≤ 20, 1 ≤ n ≤ 20).

The second line contains a string of length m of 0/1 characters: the current state of the lamps. Its j-th character (1-indexed) is 1 if lamp j is currently lit, 0 if it is off.

Each of the next n lines contains a string of length m of 0/1 characters describing one switch, in order: the i-th such line describes switch i, and its j-th character is 1 if pressing switch i toggles lamp j, 0 if it leaves lamp j alone.

Output

If no subset of switches turns every lamp off, print a single line containing -1.

Otherwise, print the minimum number of switches k needed on the first line. If k > 0, print on a second line the indices of the chosen switches, in increasing order and separated by single spaces: the lexicographically smallest such minimum-size subset (if k = 0, because all lamps already start off, print no second line).

Notes

In the first example, pressing switch 2 alone toggles lamps 1 and 3, which are exactly the lit ones, so one press suffices.

In the second example, no combination of the two switches can ever change lamp 2's state (neither switch touches it), so the single lit lamp can never be turned off.

Example 1

Input

3 2
101
100
101

Output

1
2

Example 2

Input

2 2
01
10
10

Output

-1

Hints · reveal one at a time if you're stuck

Hint 1

Treat each switch as a bit pattern over the lamps. What single operation on those patterns describes pressing a set of switches, and what must the combined result equal for every lamp to end up off?

Hint 2

With at most 20 switches you can afford to try every subset, but compute each subset's effect incrementally from a smaller subset rather than from scratch. For ties on size, the subset containing the lowest-numbered switch in the symmetric difference of two candidates has the smaller sorted list.

report an issue Prefer your own editor? sooks prep 121 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.