Problem:
Score : 1300 points

Problem Statement

There is a square-shaped grid with N vertical rows and N horizontal columns.
We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j).

Initially, each square is either white or black.
The initial color of the grid is given to you as characters a_{ij}, arranged in a square shape.
If the square (i,\ j) is white, a_{ij} is .. If it is black, a_{ij} is #.

You are developing a robot that repaints the grid.
It can repeatedly perform the following operation:

Select two integers i, j (1 ≤ i,\ j ≤ N). Memorize the colors of the squares (i,\ 1), (i,\ 2), ..., (i,\ N) as c_1, c_2, ..., c_N, respectively. Then, repaint the squares (1,\ j), (2,\ j), ..., (N,\ j) with the colors c_1, c_2, ..., c_N, respectively.

Your objective is to turn all the squares black.
Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive.

Constraints

2 ≤ N ≤ 500

a_{ij} is either . or #.

Partial Score

In a test set worth 300 points, N ≤ 3.

Input

The input is given from Standard Input in the following format:

N
a_{11}...a_{1N}
:
a_{N1}...a_{NN}

Output

If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective.
If it is impossible, print -1 instead.

Sample Input 1

2
#.
.#

Sample Output 1

3

For example, perform the operation as follows:

Select i = 1, j = 2.

Select i = 1, j = 1.

Select i = 1, j = 2.

The transition of the colors of the squares is shown in the figure below:

Sample Input 2

2
..
..

Sample Output 2

-1

Sample Input 3

2
##
##

Sample Output 3

0

Sample Input 4

3
.#.
###
.#.

Sample Output 4

2

Sample Input 5

3
...
.#.
...

Sample Output 5

5

Solution in Java7 (OpenJDK 1.7.0) (judge verdict: Runtime Error)

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //ボールの個数
        int board = sc.nextInt();
        String[][] boardData = new String[board][board];
        //各行、列の黒マス数
        int [][] blackCount = new int[2][board];

        for (int i=0; i<board; i++) {
            for (int j =0; j<board;j++) {
                boardData[i][j] = sc.next();
            }
        }
        blackCount[0] = boardDataRowCnt(board, boardData);

        int checkAvailable = 0;
        for (int i = 0; i < blackCount[0].length; i++) {
            checkAvailable += blackCount[0][i];
        }

        //すべてのマスが白の場合は黒への塗りつぶしが不可能
        if(checkAvailable == 0) {
            System.out.println("-1");
        }

        blackCount[1] = boardDataColCnt(board, boardData);

        int ans = getMinPath(board, blackCount, boardData);

        System.out.println(ans);
    }

    private static int[] boardDataRowCnt(int boardSize,String[][] strBoardData) {
        int[] blackCount = new int[boardSize];
        for (int i=0; i<boardSize; i++) {
            int rowCount = 0;
            for (int j =0; j<boardSize;j++) {
                if (strBoardData[i][j].equals("#")) {
                    rowCount++;
                }
            }
            blackCount[i] = rowCount;
        }
        return blackCount;
    }

    private static int[] boardDataColCnt(int boardSize,String[][] strBoardData) {
        int[] blackCount = new int[boardSize];
        for (int i=0; i<boardSize; i++) {
            int colCount = 0;
            for (int j =0; j<boardSize;j++) {
                if (strBoardData[j][i].equals("#")) {
                    colCount++;
                }
            }
            blackCount[i] = colCount;
        }
        return blackCount;
    }

    private static int getMinPath(int boardSize, int[][] boardData, String[][] strBoardData) {
        int minPathCount = 0;

        while(true) {
            boardData[0] = boardDataRowCnt(boardSize, strBoardData);
            boardData[1] = boardDataColCnt(boardSize, strBoardData);
            int rowNum = getMaxRow(boardSize, boardData);
            int colNum = getMinCol(boardSize, boardData);
            changeStatus(rowNum, colNum, strBoardData);
            minPathCount++;
            int cnt = 0;
            for (int i = 0; i < boardData[0].length; i++) {
                cnt += boardData[0][i];
            }
            if (cnt == Math.pow(boardSize, 2) ) {
                break;
            }
        }
        return minPathCount;
    }

    private static void changeStatus(int rowNum, int colNum, String[][] strBoardData) {
        for(int i = 0; i < strBoardData[0].length; i++) {
            strBoardData[i][colNum] = strBoardData[rowNum][i];
        }
    }

    private static int getMaxRow(int boardSize, int[][] boardData) {
        int maxNum = boardData[0][0];
        int retRowNum = 0;
        for(int i = 1;i < boardSize; i++) {
            if(maxNum > boardData[i][0]) {
                maxNum = boardData[i][0];
                retRowNum = i;
            }
        }
        return retRowNum;
    }

    private static int getMinCol(int boardSize, int[][] boardData) {
        int maxNum = boardData[0][1];
        int retColNum = 0;
        for(int i = 1;i < boardSize; i++) {
            if(maxNum > boardData[i][1]) {
                maxNum = boardData[i][1];
                retColNum = i;
            }
        }
        return retColNum;
    }
}