It is forbidden to say or think matters of sanctity in a place that contains feces or other foul smells, as it is written (Deuteronomy 23:14-15), “You will return and cover your excrement. This is because God your Lord walks among you in your camp…Your camp must therefore be holy.” This law consists of many details, and we will learn a few of them.
Anything within four amot (approximately two meters or 6.56 ft) of a person is considered to be within the confines of his camp. Hence, if there is excrement in his four amot, his camp is not holy and he is forbidden to pray there. If the excrement is in front of him, as long as he sees it, he is not allowed to pray. If the smell spreads, he must distance himself by four amot from where the smell ends. Even one whose sense of smell is impaired is required to distance himself like others who smell the odor (Shulchan Aruch 79:1).
The above law regarding human excrement applies to anything rancid whose stench causes people to be revolted. Therefore, one must distance himself from a carcass and from malodorous animal excrement, just as he must distance himself from human feces (Mishnah Berurah 79:23). Foul-smelling vomit follows the same law as excrement. But if the vomit does not smell bad, there are those who are lenient and do not consider it like feces (see Mishnah Berurah 76:20 and Ishei Yisrael 51:12).
If a person recited Shema or the Amidah within four amot of feces, he did not fulfill his obligation and the Shema or the Amidah must be repeated. Even if he was unaware that there was excrement there, but there was a fair chance that the place would contain excrement, and he neglected to check its cleanliness before beginning to pray, he did not fulfill his obligation. However, if the place was unlikely to contain excrement, b’dieved he fulfilled his obligation (Shulchan Aruch 76:8; Mishnah Berurah 31).
The poskim are divided regarding a person who recited the berachot of Keriat Shema and other berachot within four amot of excrement. Some say that because he transgressed a biblical prohibition, he did not fulfill his obligation and he must go back and recite the berachot again (Mishnah Berurah 185:7; Bei’ur Halachah there). Others maintain that it is only necessary to be stringent concerning the recital of Shema and the Amidah, but concerning other berachot, b’dieved he fulfilled his obligation (Chayei Adam 3:33; Kitzur Shulchan Aruch 5:10; Kaf HaChaim 76:37, 185:14). Since it is a matter of dispute, one may not go back and repeat the berachah.