Extending the approach introduced in https://github.com/FEniCS/dolfinx/pull/3694 for vectors to matrices.