adding this to handle negative elements too .
let val = matrix [ i ] [ j ] ; if ( val < 0 ) { let res = k - ( Math . abs ( val ) % k ) ; if ( res != mod ) { return - 1 ; } } else { let foo = matrix [ i ] [ j ] ; if ( foo % k != mod ) { return - 1 ; } } } }