My try :

But answer does not match.

Your error is near the start of the calculation. The distance between diagonally opposite charged masses is $\sqrt{\ell^2+d^2}$ not $\sqrt{\ell^2+(\frac{d}{2})^2}$.

Therefore in your answer $d$ should be replaced by $2d$, giving $n=6$.