2 of 6 deleted 77 characters in body

MATL, 28 27 bytes

tZyZ}:w:!+-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!