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."
English reference: Vol. 3, Book 12, Hadith 2300
Arabic reference: Book 12, Hadith 2388