Let $p$ be an odd prime and $\mathbb Z\_p$ be the prime field of order $p$. Consider the matrix ring $R=M\_n(\mathbb Z\_p)$. Is there any method to count the solutions of the equation (in the ring $R$)


$$X^2=I.$$
Where $I$ is the identity matrix?



Matrices over Finite Prime Fields
Denote $k:=Z\_p$.
If $p\ne2$, the matrix $X$ is in one-to-one correspondence with a decomposition $k^n=E\_+ \oplus E\_-$, where $E\_\pm$ is the eigenspace associated with the eigenvalue $\pm1$.


Given the dimension $m$ of $E\_+$ ($n-m$ for $E\_-$), these decompositions are in one-to-one correspondence with the bases of $k^n$, quotiented by the action of $GL\_m\times GL\_{n-m}$. Therefore, their number is
$$\frac{|GL\_n(Z\_p)|}{|GL\_m(Z\_p)|\cdot|GL\_{n-m}(Z\_p)|}=p^{m(n-m)}\frac{(p-1)\cdots(p^n-1)}{(p-1)\cdots(p^m-1)(p-1)\cdots(p^{n-m}-1)}.$$
Summing up over all the possible $m$'s, the number of solutions to $X^2=I$ is
$$\sum\_{m=0}^np^{m(n-m)}\frac{(p^{m+1}-1)\cdots(p^n-1)}{(p-1)\cdots(p^{n-m}-1)}.$$


