Sunan Ibn Majah, Book 12, Hadith 164 (Ibn Majah 2300)

It was narrated from Abu Sa'eed that the Prophet (ﷺ) said:

"When you come to a shepherd, call him three times. If he answers (all well and good), otherwise drink (milk from the flock) without taking advantage. And when you come to a garden call the owner of the garden three times. If he answers (all well and good), otherwise eat (from the produce of the garden) without taking advantage."