All perfect praise be to Allah, the Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad, sallallaahu ‘alayhi wa sallam, is His lave and Messenger.
The basic principle is that it is obligatory to save every life that is protected by the Shariah as long as the person is able to do so.
The author of Kashshaaf Al-Qinaaʻ said, "It is incumbent on the Muslim to rescue the non-Muslim who is protected by the Shariah, being a Thimmi (non-Muslim resident in the Muslim state) or a non-Muslim bound by a peace treaty or covenant of security with Muslims or the like, from falling into a well, being attacked by a snake, drowning, or from a fire and the like, just as it is required of him to rescue his fellow Muslim from such harms..."
If it is possible to save everyone, it is obligatory to do so. If it is impossible to save everyone and there is no other way but to save some people only, there is no doubt that saving the greatest number possible of the people whose lives are protected by the Shariah is given priority over saving one such person, in accordance with the religious principle of realizing the greater of two benefits if both cannot be realized and enduring the lesser of two evils so as to ward off the greater evil.
Shaykh Al-Islam Ibn Taymiyyah wrote, "Benefits should be realized and perfected, and evils should be eliminated and reduced; in case of a clash, realizing the greater of two benefits or enduring the lesser of two evils is the prescribed action in this regard." [Majmoo' Al-Fataawa]
He also wrote, "This is why the Shariah prescribes in case of a conflict of the benefits and evils that the greater of the two benefits must be realized by leaving the lesser benefit (when both cannot be realized) and the lesser of the two evils may be endured in order to ward off the graver evil."
Allah knows best.