Black-and-white Square
InterviewTime limit1sMemory limit256 MB
Decide whether the black cells equal the union of some full rows and columns and print the smallest stripe set.
- Level
Medium5 of 10
- Topics
- Matrix, Implementation
- Solved
- No attempts yet
Problem
The painter Quasimir Malevich painted a picture called "Black-and-white square". The picture is a rectangle filled with black cells and white cells.
Art historian Erik Stripeson proposed a hypothesis about how Malevich painted it. The canvas was white at the start, and the painter drew black stripes one cell wide, each running from one edge of the canvas to the opposite edge, either horizontally or vertically.
The Association of Computational Art Studies wants to find out whether this hypothesis can be correct. If it can be correct, the association also wants the minimal number of stripes needed to paint the picture, and a way to paint it with that many stripes.
Input
The first line contains the height and the width of the picture ().
Each of the next lines contains numbers separated by spaces. The number 0 is a white cell and the number 1 is a black cell.
Output
If the hypothesis cannot be correct, print only -1.
If the hypothesis can be correct, print on the first line the minimal number of stripes .
On the second line print numbers in increasing order, separated by spaces, describing a way to paint the picture. A positive number means drawing a vertical stripe in column (columns are numbered from the left, starting with 1), and a negative number means drawing a horizontal stripe in row (rows are numbered from the top, starting with 1).
If several sets of stripes paint the picture, print the one whose increasing sequence is smallest in lexicographic order. When is 0, the second line is empty.