custom struturing element

How do I build a custom flat struturing element assinging a different number to each of its cells?

Have you considered using FlatStructuringElement.FromImage?