The person at the back can't be saved, and thus counts the number of each color hat in front of him. Since he sees 99 hats, there must be an odd number of one color and an even color of another. He says whichever color has an odd number. After that, the next person looks up and sees 98 hats. There will be either even numbers of both or odd numbers of both. If even numbers, he knows that he had one of the hats that the first person saw as an odd number. Otherwise, he knows he has the opposite color of what the first person said. In this manner, the prisoners keep track of what the people before them have said, and whether the numbers of hats are odd or even, and each can guess his own color hat.