Final "m" before consonants becomes an Anusvāra (ṁ). (spelled separately, together for compound words) Optionally before consonants also the class nasal. (spelled together)
Final "m" before vowels stay "m". (spelled together)
Final sound
Initial sound
Sandhi
-m
vowel-
-m-vowel-
-m
consonant-
-ṁ_consonant-
(alternatively also class nasal [n/m/ṅ/ñ/ṇ] according to the following consonant - then spelled together.)
Within compound words (according to some speakers of Sanskrit also at the end of the word), the resulting Anusvāra is pronounced as the class nasal in accordance with the initial sound of the following word.