Invert a matrx of arbitrary dimension