The Prophet ﷺ said, "When you come upon a shepherd, call out to him three times. If he answers you, well and good. Otherwise, drink without causing any damage. And when you come upon the wall of an orchard, call out to the owner of the orchard three times. If he answers you, well and good. Otherwise, eat without causing any damage."
حدثنا محمد بن يحيى حدثنا يزيد بن هارون أنبأنا الجريري عن أبي نضرة
عن النبي ﷺ قال إذا أتيت على راع فناده ثلاث مرار فإن أجابك وإلا فاشرب في غير أن تفسد وإذا أتيت على حائط بستان فناد صاحب البستان ثلاث مرات فإن أجابك وإلا فكل في أن لا تفسد
[Its chain of transmission is daif] Ahmad reported it (3/21) from Yazid bin Harun, with this chain, and Hakim graded it sahih (4/132), saying it met the conditions of Muslim. Busiri said, this chain is daif, containing Jareeri, whose name was Saeed bin Iyas, and he became confused in his memory toward the end of his life, and Yazid bin Harun narrated from him after this confusion had set in. See the following hadith.