change m and n round-up
add one terminate case