By using diodes, both the ghosting and masking problems are eliminated. Just put diodes in series with each switch, like so:
Figure 17. Schematic with Diodes
Let's go back to the scenario where A, B, and D are pressed simultaneously. Activating columns C1 and C2 now look like this:
Figure 18. Only Row 1 is Activated
Figure 19. Row 1 and 2 are Activated
Voila! The diodes stop the current from flowing in the "wrong" direction back up switch B. When column C1 is activated, only node C1R1 is activated and C1R2 is not. Also, when column C2 is activated, both R1 and R2 as expected.
This fixes the masking problem for the same reason, the diode stops the current.