finished magic square function