update KM

from O(n^4) to O(n^3)