my solution, works with randomly generated test matrices

Placed lots of comments, too. Hope you'll understand my train of thought, it's really close to the vector example with just some slightly modified names for variables and methods.