I am looking for a formula for the number of possible invertible matrix keys that can be used for a Hill Cipher. From what I can tell the formula a^n^2
(where a is the alphabet size and n is the matrix key size) will provide the total number of possible matrices however this is an overestimation because many matrices will not have an inverse due to either having 0 determinant or having a determinant that is coprime with the alphabet size.
edit: in my use case the alphabet size, a, is prime
Thanks for any help,