#P1391. 方阵安排

方阵安排

Description

Class A wants a good result in the school marching competition, and they hope their class’s marching formation is a perfect square formation. They consider the formation perfect if, for every person, the number of boys among the four adjacent neighbors (up, down, left, right) is even, considering only neighbors within the grid.

You are given Class A’s current formation. You need to change as few girls as possible into boys to make the formation perfect.

Input Format

The first line contains a positive integer nn, meaning the formation is of size n×nn \times n.

Lines 22 to (n+1)(n+1) each contain nn numbers, each either 00 or 11. In the (i+1)(i+1)-th line, the jj-th number represents the gender of the person at row ii, column jj, where 00 means girl and 11 means boy.

Output Format

Output a single number: the minimum number of girls that need to be changed into boys. If there is no solution, output 1-1.

3
0 0 0
1 0 0
0 0 0

3

Hint

Explanation for Sample 1

Change the formation to:

0 1 0
1 0 1
0 1 0

Constraints

For 40%40\% of the testdata, n6n \leq 6.

For 100%100\% of the testdata, 1n181 \leq n \leq 18.

Translated by ChatGPT 5