Commutative matrix subalgebras and length function