Ghostbusters

Given independent button press probabilities, find the most likely set of pressed buttons that produces the observed row-to-column connectivity signals.

Hard8GraphDynamic programmingProbabilityMathNo attempts yetTime limit2sMemory limit512 MB

Problem

A keyboard is a grid of MM rows and NN columns of buttons. Every row has one cable and every column has one cable. Pressing the button in row ii and column jj connects the cable of row ii with the cable of column jj.

The firmware detects presses by sampling. It sends an electric signal into the cable of row 0. The signal spreads to every column cable joined to that row by a pressed button, then to every row cable joined to those columns by a pressed button, and so on. Every cable connected to the starting row through pressed buttons, directly or indirectly, receives the signal. The firmware records which columns received it, and repeats the whole process for every row.

A single press is easy to locate, because exactly one pair (row, column) makes contact. A keyboard also allows several buttons at once, and some combinations cannot be told apart. This effect is called ghosting. On a 2×22 \times 2 keyboard, for example, every combination of three or four presses connects all four cables, so the recorded signals are identical.

Connected cables in a keyboard

Four examples of connected cables. Bold lines of the same colour mark cables joined by pressed buttons, drawn as red dots. The two sets on the right cannot be told apart, because they connect the same rows and the same columns.

Each button has its own probability of being pressed, and the buttons are pressed independently of each other. The probability of a set SS of pressed buttons is the product of pijp_{ij} over the buttons in SS, times the product of 1pij1 - p_{ij} over the buttons outside SS. Given the recorded signals, find the set of pressed buttons that could produce them and has the largest probability.

Input

The first line has two integers MM and NN, the number of rows and the number of columns. (1M,N5001 \le M, N \le 500)

Each of the next MM lines has NN real numbers. The jj-th number on the ii-th line is the probability pijp_{ij} that the button in row ii and column jj is pressed. (0<pij<0.50 < p_{ij} < 0.5) Rows and columns are numbered from 0, so 0iM10 \le i \le M-1 and 0jN10 \le j \le N-1.

Each of the next MM lines starts with an integer kk (0kN0 \le k \le N) followed by kk integers. Those kk integers are the columns that received the signal sent into row ii.

The recorded signals always come from some real set of pressed buttons.

Output

Print the set of pressed buttons with the largest probability. For each pressed button print one line with two integers rr and cc, separated by a space: the row rr and the column cc of the button. Print the lines in increasing order of rr, and in increasing order of cc among equal rr.

If several sets reach the largest probability, print the lexicographically smallest one. Compare the sequences of (row, column) pairs sorted in the order above, and take the set whose first differing pair is smaller. If no button is pressed, print nothing.