Let $A$ be a diagonalizable matrix of order $n$ over a field $\mathbb{F},$ with characteristic polynomial
$$(x-c_{1})^{d_{1}}(x-c_{2})^{d_{2}}\cdot\cdot\cdot(x-c_{k})^{d_{k}}$$ where $c_{1},c_{2},\cdot\cdot\cdot c_{k} $ are distinct eigenvalues of the matrix $A.$ Now there is a formula that the dimension of the vector space of all matrices $B$ such that $AB=BA$ is $$d_{1}^{2}+d_{2}^{2}+\cdot\cdot\cdot d_{k}^{2}$$ I am trying to find this formula. How to think about it? Clearly $A$ is similar to a diagonal matrix $D$ with diagonal entries as eigenvalues. Is it correct to find dimension of the space of all matrices $B$ such that $DB=BD$? If its okay then we get the same dimension as written above. If its okay then how? Please describe me. Thanks in advance.