Magic Square Algorithm