This page is still under construction.

Parts of this page are still being built. What you see may change.

Painting

Time limit5sMemory limit128 MB

Summary
Given an n by n grid painted with k colors, find the minimum number of whole row or column repaints needed to make the entire grid one color, where a line can only be painted color c if it already has at least two tiles of that color, and list all colors achieving that minimum.
Level

Hard8 of 10

Topics
Graph, BFS, Simulation
Solved
No attempts yet

Problem

There is a wall made of n×nn \times n tiles. Long ago each tile was painted in one of kk colors. The paint has worn out, so the wall must be repainted, and this time every tile must end up in a single one of the kk colors.

In one move you may repaint one whole horizontal row or one whole vertical column of tiles into a color of your choice. However, you may paint a row or a column into color cc only if at least two of its tiles are already color cc (either from the original painting or from an earlier move).

Every tile must be repainted (covered by at least one move), and in the end all tiles must share the same color. Determine the minimum number of moves needed, and for which target colors that minimum is achieved.

Input

The first line contains the number of test cases. For each test case, the first line contains two integers nn and kk with 1<n≤5001 < n \le 500 (the number of tiles in a row) and 1≤k<n1 \le k < n (the number of available colors). Each of the next nn lines contains nn integers between 11 and kk, giving the original color of every tile.

Output

For each test case, print two lines. The first line contains qq, the minimum number of moves needed to repaint the whole wall into a single color. The second line lists, in increasing order and separated by single spaces, all colors into which the wall can be repainted in exactly qq moves. If it is impossible to repaint the wall under the rules for any color, print a single line containing just 00.

Examples1

  1. Example 1

    Input
    2
    3 2
    1 2 1
    2 1 1
    1 2 2
    2 1
    1 1
    1 1
    
    Expected output
    4
    1
    2
    1