This is the wording of our teacher, the Beit Yosef, in section 8 (Shulchan Aruch Yoreh Deah 375:8): One whose relative died, and he did not know until he arrived at the place where the deceased died or at the place of burial, if he was in a nearby place which is a journey of ten parsaot such that it is possible to arrive in one day, even if he arrived on the seventh day, if he found comforters with the head of the household, even if they had shaken themselves to stand up, since he found comforters—meaning that they are still practicing some mourning—it counts for him, and he counts with them the completion of thirty days. And if he did not find comforters, he counts for himself. And so too if he was in a distant place, even if he arrived on the second day, he counts for himself seven and thirty from the day he arrived. In what case are these matters stated? When he did not hear that he died until he arrived. But if it became known to him on the second day and he began to mourn, he does not shorten his mourning because he arrived to be with them. Thus far his words. And our teacher, the Rema, wrote on this: And some say that even if one arrived, and the head of the household is not in the house but rather went for the needs of the deceased, if he returned within three days, it is as if he is in the house. And one who arrives within three days counts with them. And one should be lenient like this opinion. And anyone whose law is to count with them, even if he returned afterwards to his own home, he counts with them. Thus far his words. And all this is when the mourners are sitting in the place where the deceased died, or in the place of his burial. But if they are sitting in another place, this one who arrives does not count with them in any case, but rather counts alone (Shach, subsection 13). And there is one who says that a head of the household who went for the needs of the deceased and returned here even on the seventh day and counts with the mourners who are here (for example, if he also began with them when the place of burial was not very distant, and look closely and you will find it easy)—also that one who arrived from a nearby place and did not know counts with them (ibid.). And it is simple that a head of the household who arrived from a nearby place and did not know, that he counts for himself (ibid.). And so too in a place where there is no head of the household at all, the one who arrives also counts for himself. And who is called the "head of the household" has been explained in section 9. (The Shach elaborated at great length, and we have explained the main points.)
וזה לשון רבינו הבית יוסף בסעיף ח (שולחן ערוך יורה דעה שעה, ח): מי שמת לו קרוב, ולא ידע עד שבא למקום שמת שם המת או למקום קבורה, אם היה במקום קרוב שהוא מהלך עשר פרסאות שאפשר שיבוא ביום אחד, אפילו בא ביום השבעה, אם מצא מנחמים אצל גדול הבית אפילו שננערו לעמוד, הואיל ומצא מנחמים דהיינו שנוהגים עדיין קצת אבלות – עולה לו, ומונה עמהם תשלום שלושים יום. ואם לא מצא מנחמים – מונה לעצמו. וכן אם היה במקום רחוק, אפילו בא ביום שני – מונה לעצמו שבעה ושלושים מיום שבא. במה דברים אמורים? כשלא שמע שמת עד שבא. אבל אם נודע לו ביום השני והתחיל להתאבל – לא יקצר אבלותו בשביל שבא אצלם. עד כאן לשונו. וכתב על זה רבינו הרמ"א: ויש אומרים דאפילו אם בא אחד, ואין הגדול בבית רק שהלך לצורך המת, אם חזר תוך שלושה ימים – הוי כאילו הוא בבית. והבא תוך שלושה – מונה עמהם. ויש להקל כסברא זו. וכל שדינו למנות עמהם, אפילו אם חזר אחר כך לביתו – מונה עמהם. עד כאן לשונו. וכל זה הוא כשהאבלים יושבים במקום שמת המת, או במקום קבורתו. אבל אם יושבים במקום אחר – אין זה הבא מונה עמהם בכל ענין אלא מונה לבדו (ש"ך סעיף קטן י"ג). ויש מי שאומר דגדול שהלך לצורך המת וחזר לכאן אפילו ביום שבעה ומונה עם האבלים שבכאן (כגון שהתחיל עמהם גם כן כשמקום הקבורה לא היה רחוק הרבה, ודייק ותמצא קל) – גם אותו שבא ממקום קרוב ולא ידע מונה עמהם (שם). ופשוט הוא דגדול הבית שבא ממקום קרוב ולא ידע, דמונה לעצמו (שם). וכן במקום שאין כלל גדול הבית, גם כן הבא מונה לעצמו. ומי נקרא "גדול הבית" נתבאר בסעיף ט. (הש"ך האריך מאוד, ועיקרי הדברים בארנו.)