Find all functions $f: \mathbb{Z} \rightarrow \mathbb{Z}$ that satisfy

$$
f(m+n)+f(m n-1)=f(m) f(n)+2
$$

for all $m, n \in \mathbb{Z}$.
First, assume there is a $c \in \mathbb{Z}$ such that $f(n)=c$ for all $n$. Then we have $2c = c^2 + 2$, so $c^2 - 2c + 2 = 0$, which has no solution for $c$. Therefore, $f$ is not constant. Now substitute $m=0$. This gives $f(n) + f(-1) = f(n) f(0) + 2$, from which we conclude that $f(n)(1 - f(0))$ is a constant. Since $f(n)$ is not constant, it follows that $f(0) = 1$. From the same equation, we now get $f(-1) = 2$. Substitute $m=n=-1$, which gives $f(-2) + f(0) = f(-1)^2 + 2$, from which it follows that $f(-2) = 5$. Next, substitute $m=1$ and $n=-1$, then we get $f(0) + f(-2) = f(1) f(-1) + 2$, from which it follows that $f(1) = 2$.
Now substitute $m=1$, then we get $f(n+1) + f(n-1) = f(1) f(n) + 2$, or

$$
f(n+1) = 2f(n) + 2 - f(n-1).
$$

By induction, we can easily prove that $f(n) = n^2 + 1$ for all $n \geq 0$ and subsequently with the equation

$$
f(n-1) = 2f(n) + 2 - f(n+1)
$$

also for $n \leq 0$. Therefore, $f(n) = n^2 + 1$ for all $n$ and this function satisfies the conditions.