The "M" comes from the trend in the 60's and 70's to take old pre-war Martin archtops and harvest the Brazilian body to convert to a flat top. Some re-used necks, others did not. David Bromberg used a guitar such as this for quite a while. I always assumed that's where Tom Paxton got involved and played an M-38 for many years.
While this sounds terrible today, if you've ever played an old Martin archtop they sounded pretty crappy as archtops go.
Sometime much later, they simplified it to 0000 to keep it simpler, size-wise. Bigger than a 000.
Well, the D means the instrument has a "dreadnought" shape, which Martin introduced many years ago. The O guitars (single, double, triple, and quadruple) indicate progressively bigger bodies in a shape that is apparently wider at the bottom and narrower in the middle. Why the OOOO became an M escapes me. I don't know about the numbers, though 30s seems to indicate a three-piece back (wild guess on that one). What 20s indicate is another mystery. The information is probably in the on of the numerous books about Martins, but I don't care enough to go searching.