#[MATL](https://esolangs.org/wiki/MATL), 28 bytes

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

Adapted from my answer [here](http://stackoverflow.com/a/28375839/2586922).

Input is in the form

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

[__Try it online!__](http://matl.tryitonline.net/#code=dFp5Wn06cXc6ISstMXleN01Hbi8qK1g6SyNTKQ&input=WzEgMiAzOyA1IDYgNDsgOSA3IDg7IDEgMiAzXQ)