Let $R$ be a ring with identity and $A\in M_n(R)$ be an $n\times n$ matrix whose entries are all idempotent elements of $R$. It is easy to see that the equality $A^2=A$ does not necessarily hold, as is seen by taking $R=\mathbb Z_2$ and $n=2$ with $A=\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix}$.
I am searching for a necessary or sufficient condition for $A^2=A$ to be true, and would appreciate any help in this regard.