public static boolean valid_to_add(int[][] A, int i, int j, int val) {
		// Check row constraints.
		for (int k = 0; k < A.length; ++k) {
			if (val == A[k][j]) {
				return false;
			}
		}

		// Check column constraints.
		for (int k = 0; k < A.length; ++k) {
			if (val == A[i][k]) {
				return false;
			}
		}

		// Check region constraints.
		int region_size = (int) sqrt(A.length);
		int I = i / region_size, J = j / region_size;
		for (int a = 0; a < region_size; ++a) {
			for (int b = 0; b < region_size; ++b) {
				if (val == A[region_size * I + a][region_size * J + b]) {
					return false;
				}
			}
		}
		return true;
	}