Place one third of the potatoes in a greased 3-quart casserole or baking dish.
Sprinkle with one third of the leeks, pimientos, and flour.
Add half the ham.
Repeat, and finish with a layer of leeks, pimientos, and flour.
Season with salt and pepper - remember, the ham may be salty - and pour the milk over all.
Dot with butter and bake covered in a preheated 350F (180C) oven for 30 minutes.
Remove the cover and bake until the potatoes are tender and the liquid has thickened, 30 to 40 minutes.
Let stand for 10 minutes before serving.
Serves 4 to 6.