Under construction

Still being built. Things may change

The Chosen Sub Matrix

Interview

Time limit1sMemory limit128 MB

Summary
For each N x N matrix, find the M x M sub matrix with the fewest distinct values, breaking ties by larger descending value lists, then by smaller top-left row and column.
Level

Easy3 of 10

Topics
Brute force, Implementation, Matrix, Sorting
Solved
No attempts yet

Problem

From a given N×NN \times N matrix, find the M×MM \times M sub matrix that contains the fewest distinct values. When several sub matrices are tied on the number of distinct values, break the tie with the following rules:

  1. The sub matrix with fewer distinct values wins.
  2. If still tied, list each sub matrix's distinct values in descending order and compare these lists element by element; at the first position where they differ, the sub matrix with the larger value wins.
  3. If the sets of distinct values are identical too, choose the one whose top-left cell has the smaller row index, and if the row index is also equal, the smaller column index.

Matrix indices start at 1.

For example, consider the following 4×44 \times 4 matrix:

3 9 9 9
3 9 9 2
3 9 9 2
2 5 5 2

There are four possible 3×33 \times 3 sub matrices.

S1 (top-left 1, 1) — distinct values {9, 3}

3 9 9
3 9 9
3 9 9

S2 (top-left 1, 2) — distinct values {9, 2}

9 9 9
9 9 2
9 9 2

S3 (top-left 2, 1) — distinct values {9, 5, 3, 2}

3 9 9
3 9 9
2 5 5

S4 (top-left 2, 2) — distinct values {9, 5, 2}

9 9 2
9 9 2
5 5 2

S1 and S2 each have 2 distinct values, fewer than S4 (3) and S3 (4). Comparing S1's list (9, 3) with S2's list (9, 2), the first values match and S1 is larger at the second, so S1 ranks above S2. The overall ranking is S1, S2, S4, S3, so the chosen sub matrix is S1.

Input

The input consists of one or more test cases and continues until end of file. The first line of each test case contains two integers NN (1≤N≤101 \le N \le 10), the size of the matrix, and MM (1≤M≤N1 \le M \le N), the size of the sub matrix to choose. The next NN lines each contain NN integers separated by single spaces, describing the matrix. Every element of the matrix is an integer from 00 to 99 inclusive.

Output

For each test case, print on a single line the top-left index (row and column, separated by a single space) of the chosen sub matrix.

Examples1

  1. Example 1

    Input
    4 3
    3 9 9 9
    3 9 9 2
    3 9 9 2
    2 5 5 2
    10 2
    1 5 7 8 2 3 3 3 1 7
    2 2 3 6 3 7 3 2 3 1
    5 9 3 5 7 0 4 6 9 1
    1 0 3 4 2 6 4 3 9 0
    7 4 9 9 5 4 6 2 1 5
    5 6 9 9 6 6 3 8 0 8
    4 3 3 5 2 1 7 6 4 1
    6 5 9 5 0 3 1 8 8 6
    2 2 2 8 0 1 3 5 9 0
    3 6 4 2 3 3 0 2 0 0
    
    Expected output
    1 1
    5 3