How loyal are dogs to their owners? So loyal that if they see someone snubbing the owner, they're likely to snub that someone in return. That's the upshot of an unusual experiment in canine cognition, conducted by Japanese researchers.

The results will be published in the journal Animal Behaviour, Kyoto University said Friday in a news release.

Here's how the experiment worked: The researchers set up three groups of 18 dogs and their owners. Each dog watched a little drama unfold, involving its owner and two strangers. In one group, the owner struggled to open a box, asked for help from one of the strangers, and was rebuffed. In the second group, the stranger played nice and helped the owner open the box. In the control group, the owner interacted with neither of the strangers.

After the drama, each of the strangers would hold out a treat for the dog. In the cases where the owner was rebuffed, the dogs were far more likely to avoid the bad guy and take a treat from the other stranger. In the other cases, the dogs didn't distinguish between the strangers.

Related: Scientists Try to Get Inside a Dog's Head

Researchers at Kyoto University's Companion Animal Mind Project, led by Kazuo Fujita, said their experiment showed that dogs know enough about human behavior to take their owner's side when the owner runs into trouble with other humans. "Such third-party evaluation is one of the factors that has enabled the development of cooperative societies," they said. It's also one of the factors that the owners of guard dogs — and the folks who write scripts about hero dogs — depend on.