hej,
the code should work, maybe you connect the matrix the wrong way, according to the datasheet the ground from the LED (ctahtode) should be connected to the dig0-7 (pin 2,3,5-8,10,11)
("Eight-Digit Drive Lines that sink current from the display common cathode. The MAX7219 pulls the digit outputs to V+ when turned off. The MAX7221's digit drivers are high-impedance when turned off.")
and the supply side LED+ (annode) have to be connectet to the segA-D, DP ( pin14-17, 20-23)
(" Seven Segment Drives and Decimal Point Drive that source current to the display. On the MAX7219, when a segment driver is turned off it is pulled to GND. The MAX7221 segment drivers are high-impedance when turned off.")
furthermore if you connect the matrix the otherway arround, it will alway light, because for example when a dig pin (at the max7219) is turned off it will pulled from ground to 5v and for the sig it is the otherway around.
so maybe this is your problem, but otherwise maybe you should post some fotos and discribe your problem a little bit more
als the best
tomek