I ended up needing this so often for graph processing, and for values which might be inexact if using floating point, that I saved the formula in a blog post.
https://vladfeinberg.com/2020/03/07/subset-isomorphism.htmlThe formula can be "oblivious" to the final size of the matrix too, which is helpful if you're doing some sparse ML training on edges (e.g., GNNs).