Structural matrix algebras, generalized flags, and gradings