Optimal convex $M$-estimation via score matching