WAUKESHA, Wis. — A Wisconsin girl who stabbed a classmate in an attack inspired by the fictional horror character Slender Man was sentenced to 40 years in a mental hospital Thursday.

Judge Michael Bohren granted the maximum penalty that prosecutors had sought, and discounted Morgan Geyser’s youth — she was just 12 — at the time of the attack in 2014.

“What we can’t forget is this was an attempted murder,” Bohren said. He said he believed Geyser remained a risk to hurt herself and others, and called it “an issue of community protection.”

Geyser, now 15, spoke briefly before she was sentenced. She broke down in tears, apologizing to the girl she stabbed, Payton Leutner.

“I just want to let Bella and her family know that I’m sorry,” she said, using a nickname for Leutner.

Geyser and another girl, Anissa Weier, admitted that they lured Leutner into some woods near a suburban Milwaukee park. Geyser stabbed Leutner 19 times while Weier urged her on, according to investigators. Leutner was left for dead but she crawled out of the woods and got help from a bicyclist. All three girls were 12 at the time.

Geyser and Weier said they carried out the attack to curry favor with Slender Man, a fictional online horror character typified by spidery limbs and a blank white face.

Weier was sentenced to 25 years in a mental hospital in December. She pleaded guilty in August to being a party to attempted second-degree intentional homicide, but she claimed she wasn’t responsible for her actions because she was mentally ill. In September, a jury agreed.

Geyser pleaded guilty to attempted first-degree intentional homicide in October in a deal with prosecutors to avoid prison.