Under construction

Still being built. Things may change

Magnet Chess

Interview

Time limit1sMemory limit1024 MB

Summary
Given an N by N board of magnets and a touch distance D, find the empty cell where a new magnet touches the fewest existing magnets, or none if a cell touches zero.
Level

Medium4 of 10

Topics
Prefix sum, Matrix, Brute force
Solved
No attempts yet

Problem

Sejong, who has come over to the Joseon era, decided to teach magnet chess to people who are tired of janggi and baduk.

Magnet chess is a game for two players. Each player holds the same number of magnets. The players take turns placing one magnet at a time on an empty cell of an N×NN \times N board.

On a turn, a player places one of their magnets on an empty cell so that it does not touch any other magnet. The player who uses up all their magnets first wins. However, if the placed magnet touches other magnets, the player takes back all touching magnets, including the new one.

A cell in row rr and column cc is written (r,c)(r, c), and the top-left cell is (1,1)(1, 1). Suppose a magnet is at (r1,c1)(r_1, c_1) and a new magnet is placed at (r2,c2)(r_2, c_2). If ∣r2−r1∣≤D|r_2 - r_1| \le D and ∣c2−c1∣≤D|c_2 - c_1| \le D, the magnet at (r1,c1)(r_1, c_1) touches the new magnet.

Midway through the game, Sejong has one magnet left, and it is now Sejong's turn. Find the best position Sejong can place on this turn, to help Sejong, who has lost focus.

Input

The first line contains the board size NN and the touching distance limit DD, separated by a space. (3≤N≤10003 \le N \le 1000, 1≤D≤N1 \le D \le N)

The next NN lines describe the board. A cell with a magnet is 1, and an empty cell is 0. The jj-th number on the ii-th line tells whether there is a magnet at row ii, column jj.

The initial board has no two magnets touching.

Output

On the first line, print the position where Sejong can win on this turn, if such a position exists. If no winning position exists, print the position that takes the fewest magnets. Print the row and column separated by a space.

On the second line, if no winning position exists, print the fewest number of magnets taken.

If several positions qualify, print the one with the smallest row number. If the row numbers are the same, print the one with the smaller column number.

Examples2

  1. Example 1

    Input
    5 1
    1 0 0 0 0
    0 0 1 0 1
    1 0 0 0 0
    0 0 1 0 0
    1 0 0 0 0
    
    Expected output
    4 5
    
  2. Example 2

    Input
    5 1
    1 0 1 0 1
    0 0 0 0 0
    1 0 1 0 1
    0 0 0 0 0
    1 0 0 1 0
    
    Expected output
    5 2
    1