Use two facts:
- If $AB = BA$, then $B$ maps each eigenspace of $A$ onto itself. (If $A v = c_{i} v$, then $A (B v) = B (A v) = B (c_{i} v) = c_{i} (B v)$.)
- Restricted to the eigenspace $V_{i}$, of dimension $d_{i}$, relative to $c_i$, $A$ is a scalar matrix, so it commutes with all the $d_{i} \times d_{i}$ matrices on $V_{i}$.
Example Let $d_{1} = 2$, $d_{2} = 3$. Then
$$
A =
\begin{bmatrix}
c_1&&&\vert\\&&c_1&\vert\\\hline &&&\vert &c_2\\&&&\vert&&&c_2\\&&&\vert&&&&c_2
\end{bmatrix}.
$$
If $B$ commutes with $A$, is has the block form
$$
B =
\begin{bmatrix}
H&\vert\\\hline&\vert&K
\end{bmatrix},
$$
where $H$ is an arbitrary $2 \times 2$ matrix, and $K$ is an arbitrary $3 \times 3$ matrix. So the space of such $B$'s has dimension $d_1^2 + d_2^2 = 2^2 + 3^2 = 13$.