Taking the problem generally, we have an operator $O$ defined such that $O|k\rangle = (-1)^{f(k)}|k\rangle$ for all computational basis states $|k\rangle$ where $f(k) \in \{0, 1\}$ for all $k$, and a unitary state preparation $U$ and its inverse. Defining $G = U(2|0\rangle\langle0| - I)U^{\dagger}O$, we want to know the value $q$ such that the probability that $G^q U|0\rangle$ will be measured on the computational basis as a -1 eigenvector of $O$ is maximized.
$|\psi\rangle = U|0\rangle$ can be written in the form $\cos(\theta) |\psi_b\rangle + \sin(\theta)|\psi_g\rangle$ where $0 \leq \theta \leq \pi/2$ for two $|\psi_b\rangle$ and $|\psi_g\rangle$ that are unit eigenvectors of $O$ corresponding to 1 and -1 eigenvalues respectively. Treating this geometrically as just a unit vector on a 2D real plane, applying $O$ will reflect the vector across $|\psi_b\rangle$, and applying $2|\psi\rangle\langle\psi| - I$ will reflect the new vector across $|\psi\rangle$. Applying them in order will result in $\cos(3\theta)|\psi_b\rangle + \sin(3 \theta)|\psi_g\rangle$, and then $G^q |\psi\rangle = \cos((2q + 1)\theta)|\psi_b\rangle + \sin((2q + 1)\theta)|\psi_g\rangle$. Since the probability of measuring a good state is best if $(2q + 1)\theta = \frac{\pi}{2}$, we get what can be rounded to the nearest integer as the number of iterations, independent of any more details of $U$, $O$, and $|\psi\rangle$ other than $\theta$.
Qiskit's built-in optimal_num_iterations (source https://qiskit.org/documentation/_modules/qiskit/algorithms/amplitude_amplifiers/grover.html#Grover.optimal_num_iterations) calculates $a = \sin(\theta)$ ("amplitude") as $\sqrt{s/2^n}$ where $s$ is the number of solutions and $n$ the number of qubits, and then solves for $q = \frac{\pi}{4\theta} - \frac{1}{2} = \frac{\pi}{4 \sin^{-1}(a)} - \frac{1}{2} = \frac{\cos^{-1}(a)}{2\sin^{-1}(a)}$. The amplitude of $|\psi_g\rangle$ is guaranteed as $\sqrt{s/2^n}$ only in the uniform superposition case, so this built-in function won't work if the state preparation is different.
In the more general case, you'll need to know the value of $\theta$ beforehand, or have a procedure classically or quantum to derive it, to know the number of iterations to do. The value of $\theta$ can be worked out in general using a separate quantum phase estimation circuit to measure the period of $G$ applied to $|\psi\rangle$ with a number of additional qubits corresponding to the desired level of precision: in very small cases, this will take sufficient effort so as to invalidate the potential advantage, but asymptotically the phase estimation circuit will remain only about as large as the Grover circuit run afterwards armed with knowledge of $\theta$.