If you don't want to invoke ring theory:
Let $n\geq 2$.
For any such $f$, we have that if $A^2=A$, then $f(A)=0$ or $f(A)=1$. If $f(I_n)=0$, then $f$ is constant $0$, so we may assume that $f(I_n)=1$.
Let $E_{ij}$ be the matrix that has a $1$ in the $(i,j)$ coordinate and $0$ elsewhere. Since $E_{ii}^2 = E_{ii}$, it follows that $f(E_{ii})=0$ or $f(E_{ii})=1$.
Since $I_n = E_{11}+\cdots+E_{nn}$, we have that $f(E_{11})+\cdots + f(E_{nn})= f(I_n) = 1$. That means that exactly one of $E_{ii}$ maps to $1$, and the rest map to $0$. Suppose that $f(E_{ii}) = 1$.
Then for any matrix $A$, we have that $f(E_{ii}A) = f(AE_{ii}) = f(A)$.
If $A$ and $B$ have the same $i$th column, then $AE_{ii}=BE_{ii}$. By the above, we have $f(A)=f(B)$.
Likewise, if $A$ and $B$ have the same $i$th row, then $E_{ii}A=E_{ii}B$, so $f(A)=f(B)$. Thus, $f(A)$ is completely determined by the value of $a_{ii}$, the $(i,i)$ entry of $A$.
Let $A$ be the matrix of all $1$s. Then $A^2$ is the matrix of all $n$s. But $A$ has the same $(i,i)$ entry as $I_n$, so $f(A)=1$, which means that $f(A^2) = f(A)^2 = 1$; while $A^2$ has the same $(i,i)$th entry as $nI_n$, so $f(A^2) = f(nI_n) = nf(I_n)=n$. This is a contradiction.
So if $n\geq 2$, then the only map is the zero map. If $n=1$, then a ring homomorphism from $\mathbb{R}$ to itself must be either the zero map, or the identity.
If you know ring theory, though, there is a shorter argument (modulo knowing the ideals of a matrix ring over a ring with unity): because $f$ is a ring homomorphism, the kernel is an ideal. It is well known that the ring of matrices over a division ring is simple: the only ideals are the trivial ideal and the whole ring. If the kernel is the whole ring, then this is the zero map. If the kernel is trivial, then thinking of it as a linear map of real vector spaces, we have $\dim(\mathbb{R}^{n\times n})=n^2$ and $\dim(\mathbb{R})=1$, so $n^2=1$, thus $n=1$. Then you just need to verify that the only nonzero ring homomorphism from $\mathbb{R}\to\mathbb{R}$ is the identity map (it maps positives to positives, so it respects order, and restricts to the identity on $\mathbb{Q})$. Thus, either $f(A)=0$ for all $A$, or $n=1$ and $f=\mathrm{id}$.