The minimal polynomial of a matrix is the monic polynomial in of smallest degree such that
(1)

The minimal polynomial divides any polynomial with and, in particular, it divides the characteristic polynomial.
If the characteristic polynomial factors as
(2)

then its minimal polynomial is given by
(3)

for some positive integers , where the satisfy .
For example, the characteristic polynomial of the zero matrix is , whiles its minimal polynomial is . However, the characteristic polynomial and minimal polynomial of
(4)

are both .
The following Wolfram Language code will find the minimal polynomial for the square matrix in the variable .
MatrixMinimalPolynomial[a_List?MatrixQ,x_]:=Module[ { i, n=1, qu={}, mnm={Flatten[IdentityMatrix[Length[a]]]} }, While[Length[qu]==0, AppendTo[mnm,Flatten[MatrixPower[a,n]]]; qu=NullSpace[Transpose[mnm]]; n++ ]; First[qu].Table[x^i,{i,0,n1}] ]