This is a variant of a classical problem. Before leaving, they choose one ninja who will report back to the master. Each ninja goes and completes his mission. When he is done, he returns to the lantern. If he finds the lantern off, he turns it on. If he finds it on, he leaves without doing anything. The only ninja who is allowed to turn off the lantern is the ninja chosen to report back to the master. You should be able to figure it out from here.