adding this to handle negative elements too .
val = matrix [ i ] [ j ] NEW_LINE if ( val < 0 ) : NEW_LINE INDENT res = k - ( abs ( val ) % k ) NEW_LINE if ( res != mod ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT foo = matrix [ i ] [ j ] NEW_LINE if ( foo % k != mod ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT