I believe it is from the Prophet ﷺ who said, "When you come upon a walled garden, call out to its owner three times. If he answers you, well and good, otherwise eat without causing any damage. And when you come upon a shepherd, call out to him three times. If he answers you, well and good, otherwise eat and drink without causing any damage."He said, and the Messenger of Allah ﷺ said, "Hospitality is for three days. Whatever comes after that is charity."
حدثنا علي بن عاصم حدثنا سعيد بن إياس الجريري عن أبي نضرة
أراه عن النبي ﷺ قال إذا أتيت على حائط فناد صاحبه ثلاث مرات فإن أجابك وإلا فكل من غير أن على تفسد وإن أتيت على راع فناده ثلاث مرات فإن أجابك وإلا فكل واشرب من غير أن تفسد قال وقال رسول الله ﷺ الضيافة ثلاثة أيام فما بعد فصدقة