When one does not recognize the Imam of his time that is rightly and divinely guided, they are like lost sheep who attach themselves to other flocks of sheep and suffer deception, searching for their true Sheppard (guide and Imam) and eventually are overtaken by death, resulting in their deeds being not accepted. Imam Baqir (as) explains…

“I heard abu Ja‘far (as) say, ‘One who follows a religion in the name of Allah and worships Him assiduously without acknowledging the Imam (Leader with Divine Authority), his toiling and efforts will not be accepted.
In fact, he is lost and confused and Allah disdains his deeds. Such person’s case and example is like a sheep that has lost its shepherd and flock and runs back and forth the whole day. As the night falls, this sheep finds a flock of sheep with a shepherd. The (lost) sheep then joins the flock affectionately, that in fact is only a deception and not her own flock of sheep. The lost sheep passes the night in their shelter but when the shepherd leads the flock to the pasture the lost sheep feels lost and away from the flock and the shepherd, thus runs back and forth in search of the flock and the shepherd. Then it finds a flock of sheep with a shepherd and affectionately joins them but, in fact, suffers a deception. This shepherd shouts at the lost sheep saying, “Go and find your own flock and shepherd. You are lost, confused and you are without a flock and shepherd.” The lost sheep then runs back and forth in confusion, fear and frustration without a shepherd to guide it to the pasture and to the shelter. While the lost sheep is caught up in such confusion a wolf seizes the opportunity and kills the lost sheep for food.
So also, by Allah, O Muhammad, is the case of a person from this ’Umma (nation) who lives without an Imam (Leader) from Allah, the Most Majestic, the Most Gracious, the Imam whose Divine Authority is clearly supported with evidence and who is just in his dealings. A person without such Imam is lost and confused and if he dies in such condition, his death is like dying in disbelief and hypocrisy. O Muhammad, bear in mind properly that the unjust ‘A’immah (leaders without Divine Authority) and their followers are far away from the religion of Allah. They are lost and misleading. Their deeds that they do are like the dust blown by the winds away in a windy day, which go out of hand as useless matters. Thus is straying far away from the truth.’”

Note: The last line from Imam Baqir (as) reflect the words of our Lord, Might and Majestic – “The example of those who disbelieve in their Lord is [that] their deeds are like ashes which the wind blows forcefully on a stormy day; they are unable [to keep] from what they earned a [single] thing. That is what is extreme error.” (14:18)

Sanad: Muhammad ibn Yahya has narrated from Muhammad ibn al-Husayn from Safwan ibn Yahya from al-‘Ala’ ibn Razin from Muhammad ibn Muslim who has said the above hadith

Source: Al-Kafi, Volume 1, Hadith #964

Grading: Allaamah Majlisi (rh) grades it Sahih/Authentic (صحيح) [Mira’at Al-Uqool, Volume 4, Page 214 (See Image)]

محمد بن يحيى، عن محمد بن الحسين، عن صفوان بن يحيى، عن العلاء بن رزين عن محمد بن مسلم قال: سمعت أبا جعفر عليه السلام يقول: كل من دان الله بعبادة يجهد فيها نفسه ولا إمام له من الله فسعيه غير مقبول، وهو ضال متحير والله شانئ لاعماله ومثله كمثل شاة ضلت عن راعيها وقطيعها، فهجمت ذاهبة وجائية يومها، فلما جنها الليل بصرت بقطيع مع غير راعيها، فحنت إليها واغترت بها، فباتت معها في ربضتها فلما أن ساق الراعي قطيعه أنكرت راعيها وقطيعها، فهجمت متحيرة تطلب راعيها وقطيعها، فبصرت بغنم مع راعيها، فحنت إليها واغترت بها، فصاح بها الراعي الحقي براعيك وقطيعك، فإنك تائهة متحيرة عن راعيك وقطيعك، فهجمت ذعرة متحيرة نادة لا راعي لها يرشدها إلى مرعاها أو يردها، فبينا هي كذلك إذا اغتنم الذئب ضيعتها فاكلها، وكذلك والله يا محمد من أصبح من هذه الأمة لا إمام له من الله عز وجل ظاهرا عادلا أصبح ضالا تائها وإن مات على هذه الحال ما ت ميتة كفر ونفاق، واعلم يا محمد أن أئمة الجور وأتباعهم لمعزولون عن دين الله، قد ضلوا وأضلوا، فأعمالهم التي يعملونها كرماد اشتدت به الريح في يوم عاصف لا يقدرون مما كسبوا على شئ ذلك هو الضلال البعيد