Solution to matrix rotation