Write a function to perform matrix multiplication from scratch, and explain how you would optimize its computational complexity.