We cannot pray PUBLICLY for those who have died outside the Church.
We can pray privately for them ... with this mindset. We know that God isn't bound by time and can foresee and possibly take into account future prayers offered for a person at the moment of their death. God can send an angel or bilocate a saintly priest to the site in order to confect the Sacrament if need be. Also, these prayers can win certain actual graces which could help mitigate the person's eternal punishment. In the end, prayers are never wasted. If the prayers do not have any effect for that person, God will still take them into account somehow. Provided one does not pray for these souls as if there were any objective hope of their salvation if they were to have died outside the Church. Again, even if the person may have appeared to die outside the Church and without Baptism, there's absolutely no limit to the power of God in providing these things even miraculously if need be. We have stories where saints raised people back to life in order to baptize them.