Nancy Reagan had a flamenco lesson today and afterward Spain's Queen Sofia, who added the outing to her schedule at the last minute, gave her a rave review.

"I think she had great rhythm -- fantastic!" the queen said of Mrs. Reagan's impromptu performance at the Royal Theatre, where every tap of the First Toe was filmed by Spanish and American television crews.

Sources close to the royal family here said the queen averted a diplomatic incident by taking charge of the day's events for Mrs. Reagan. The queen originally had been scheduled only to participate in a visit to the Prado Museum and a luncheon afterward. She reportedly feared Mrs. Reagan would not be properly accompanied after Carmen Romero de Gonzalez, the wife of Prime Minister Felipe Gonzalez, said she was too busy to act as the "official hostess" for the day's events.

After that, one of the king's sisters, the Duchess of Badajoz, was enlisted to stand in as the official hostess. But by this time Queen Sofia stepped in and let everyone know that she was taking charge. She invited the prime minister's wife to attend the luncheon, and when the queen is the hostess, it's a command performance.

The flamenco "lesson" turned out to be the beginning of a crash course for the first lady in Spanish art, culture and cuisine. Her next stop was the Prado, where she saw works by Vela'zquez and Goya, two of Spain's greatest painters. From there she and the queen went to lunch at Madrid's oldest restaurant, Casa Botin, and sat in the same spot that the late Ernest Hemingway used to favor when he dined there.

Only in the narrow, cobblestone street in front of the restaurant in the Old City did Mrs. Reagan meet any negative reaction to President Reagan's visit to Spain. While Madrilen os lined the sidewalk to watch the arrival of the two women, and others leaned over their wrought iron balconies to shout "Viva," a lone banner of dissent proclaimed in Spanish "Reagan Out" and "Nicaragua Will Triumph." Mrs. Reagan just smiled and waved to everybody.

The flamenco lesson followed a demonstration of flamenco steps by three classes of dancers, ranging in age from 9 to 17 years, enrolled in the Royal School of Dance and Dramatic Arts. Queen Sofia and Carmen Romero de Gonzalez saw what was coming and started laughing when the dancing master held out his hands inviting Mrs. Reagan to try the basic steps.

Looking somewhat apprehensive but smiling through it all, the first lady agreed. As the distinctive flamenco beat filled the hall she began moving her feet from front to back and waving her hands windmill-fashion. Though doubtless hampered by the absence of castanets, her footwork showed promise and her movements were fluid.

If the applause wasn't exactly deafening, it was only because the theater was empty and her audience, aside from Queen Sofia and the official party, consisted of photographers and reporters too busy recording the minute-long performance for history. There was general agreement later, however, that the first lady's footwork bore a distinct resemblance to flamenco even if her rust and brown suit with matching leather shoes weren't what your best-dressed flamenco dancers usually wear.

Later, at the Prado, when a reporter told Mrs. Reagan she was quite a flamenco dancer, she laughed and protested, "Oh, no, no. I think you have to start very young." Asked if the lesson was a surprise, she said, "It certainly was."

Her press secretary, Jennefer Hirshberg, said that the Spanish had inquired whether they might ask Mrs. Reagan to try the dance but nothing had been decided. "We were going to let her play it by ear," Hirshberg added.

The prime minister's wife, reportedly quite a flamenco dancer herself, avoided any long-range speculation about Mrs. Reagan's future in the art. She called the first lady's performance "fun" but said, "I think you better ask her," when questioned about what could lie ahead in the tap-shoe department for the former Hollywood actress.

At the Prado, where both Queen Sofia and a curator provided a running commentary, the first lady saw Vela'zquez's "Las Lanzas" (the "Surrender of Breda") and "Las Menin as" (the famous rendering of the family of Philip IV), as well as Goya's the "Family of Charles IV." Queen Sofia, at one point, showed Mrs. Reagan how Vela'zquez used "to paint and rub out and paint and rub out, and you can see that the horse has four legs in the back where there should only be two."

About 30 children from the American School accompanied Mrs. Reagan and the queen on their Prado rounds. When they presented flowers to her, one of them asked about the First Puppy. Mrs. Reagan said Lucky is in obedience school at present but that she and the president had received a letter from her.

"It said, 'Dear Mom and Dad, I'm having a good time. I'm meeting a lot of new friends,' and," Mrs. Reagan continued, "she put her paw prints on it."

In front of a Goya painting called "Vincente Lopez," Mrs. Reagan told the Prado's curator that her great-great grandmother was married to a Spaniard. "On that side of the family they were all blue-eyed and blond-haired and then I came along and everyone wondered where I came from," she said.

In a newly restored gallery of privately owned Goyas, she saw one work titled "La Duquesa de Alba y su Duen a," in which the duchess leans over a woman dressed in severe clothing and holding a cross. "She looks like she's trying to kill her," Mrs. Reagan said. "The expression on her face -- she's trying to do something to her."

Later, at Casa Botin, Mrs. Reagan said she had been in Madrid in 1972 when President Nixon sent her husband here on a mission. Noting that some of the paintings she saw were from the "dark period," she said she saw some paintings she had never seen before. "Can you imagine living with those pictures in your living room?" she asked. And she said that her favorite painting was a small Vela'zquez, impressionistic in style.

Dining with Mrs. Reagan (who ordered gazpacho and sole instead of the house specialities, roast suckling pig and roast baby lamb) and Queen Sofia were King Juan Carlos' other sister, the Duchess of Soria (Margarita), who is almost totally blind; Maria Teresa de Manueco, wife of the Spanish ambassador to the United States and Gaetana Enders, wife of the U.S. ambassador to Spain.

The Reagans arrived from Bonn Monday afternoon, and Monday night dined with King Juan Carlos, Queen Sofia and their daughters, Princesses Elena and Cristina.