private static boolean magicSquare(int[][] matrix) {
		int n = matrix.length;
		int nSquare = n * n;
		int magicSum = (n * n * (n * n + 1) / 2) / n; // Distribution of first k
														// = n * n natural
														// numbers distributed
														// among n rows/columns
		int rowSum = 0, colSum = 0, firstDiagonalSum = 0, secondDiagonalSum = 0;
		boolean[] flag = new boolean[n * n];

		for (int row = 0; row < n; row++) {
			rowSum = 0;
			colSum = 0;
			for (int col = 0; col < n; col++) {
				if (matrix[row][col] < 1 || matrix[row][col] > nSquare)
					return false;
				if (flag[matrix[row][col] - 1] == true)
					return false;
				flag[matrix[row][col] - 1] = true;
				rowSum += matrix[row][col];
				colSum += matrix[col][row];
			}
			firstDiagonalSum += matrix[row][row];
			secondDiagonalSum += matrix[row][n - row - 1];
			if (rowSum != magicSum || colSum != magicSum)
				return false;
		}
		if (secondDiagonalSum != magicSum || secondDiagonalSum != magicSum)
			return false;
		return true;
	}