A rank characterization of the number of final classes of a nonnegative matrix