Allocate temporary matrices in heap instead of stack.