cho.sh
Notes
Loading...

Lighting Fixture

Time limit

2s

Memory limit

128 MB

Problem

Chulsoo has a lighting fixture whose lamps are arranged in an N × M grid. Each lamp is either red or white. As the lighting director, Chulsoo can change the fixture with the following two kinds of buttons.

  • Each row has one row button. Pressing a row button changes the color of every lamp in that row: red becomes white, and white becomes red.
  • Each column has one column button. Pressing two different column buttons at the same time swaps those two entire columns while preserving the order of lamps inside each column.

For example, suppose the fixture is the 4 × 5 grid shown in (a) below. 0 means white and 1 means red. If the second row button is pressed in (a), the fixture becomes (b). If the second and fourth column buttons are then pressed together, those columns are swapped and the fixture becomes (c).

```
01001
10110
01110
10101
``````
01001
01001
01110
10111
``````
00011
00011
01110
11101
|
| (a)                             | (b)                             | (c)                             |
Given the initial colors and the target colors of an N × M lighting fixture, find an order of row-button and column-button operations that transforms the initial state into the target state.

Input

The first line contains two integers N and M, the number of rows and columns of the lighting fixture.

The next N lines describe the initial state. Each line contains M values for the colors in that row.

The following N lines describe the target state in the same format.

Each value is either 0 or 1. 0 means white, and 1 means red.

Output

If the initial state cannot be transformed into the target state, print -1 on the first line.

Otherwise, print the number of operations S on the first line. S is the sum of R, the number of row-button presses, and C, the number of column swaps.

Then print S lines, one for each operation in the order they are performed.

  • To press the row button for row i, print 0 i.
  • To swap columns i and j, print 1 i j.

The same row button may be pressed at most twice, and the same pair of column buttons may also be pressed at most twice.

Constraints

  • 1 ≤ N ≤ 256
  • 1 ≤ M ≤ 256
| (a)                             | (b)                             | (c)                             |
Given the initial colors and the target colors of an N × M lighting fixture, find an order of row-button and column-button operations that transforms the initial state into the target state.