1 of 6

MATL, 28 bytes

tZyZ}:qw:!+-1y^7MGn/*+X:K#S)

Adapted from my answer here.

Input is in the form

[1 2 3; 5 6 4; 9 7 8; 1 2 3]

Try it online!