When Beyonce released her latest album, “Lemonade,” many saw the project — particularly a tender ballad about reconciliation and forgiveness titled “Sandcastles” — as confirmation that tabloid speculation about her husband Jay Z's infidelities was true.

It was, after all, one of the album's most emotional tracks. In her film released as a companion to the album (also titled “Lemonade”), “Sandcastles” featured Jay embracing his tearful wife and lying at her feet, the most intimate of displays for pop's most intensely private couple.

“Sandcastles,” however, was originally about another man's heartache.

Vincent Berry II promised himself it would be the final song he'd write about his ex-girlfriend. Little did the songwriter know at the time that such a painful chapter of his life would translate into the most successful moment of his career.

Berry was homeless when the song made its way to Beyonce last year, and uncertain how, or if, “Sandcastles” would ever be used.

“When someone sings your song, it's incredible,” Berry said. “But when the biggest artist in the world sings your song, it's really a defining moment for yourself that you know you're supposed to be doing what you're doing.”

The idea for the song came when Berry, 30, was working with poet-rapper-actor Malik Yusef, who pitched an idea for a song called “We Built Sandcastles That Washed Away” early last year.

Gathered at a piano, Yusef sang some initial gospel-influenced lyrics that he'd come up with, words that “opened a wound” for Berry.

“I hadn't really healed,” Berry recalled of a 10-year relationship that ended long ago. “When he sang the words, I just thought about her, and it was all I needed to get the rest out.”

Berry took the song's basic framework and began to build. “I said, ‘You know what, this is my last time writing a song about this girl,'?” he recalled. “I'm putting it all in this.”

The song was originally intended for R&B singer Teyana Taylor, although Berry believed it needed Beyonce's voice, even if he'd never met the superstar.

So how did it reach Beyonce? Old-fashioned word of mouth.

Berry got invited to another studio session where he was introduced to another songwriter, Midian Mathers. He played her the record. “When she heard it, she started crying,” he said.

The trio developed the song further, and Mathers sang it at a showcase. Later, Mathers had a meeting at music publishing company BMG and connected with A&R executive Teresa LaBarbera Whites, who famously discovered and signed Beyonce and Destiny's Child to Columbia Records.

When Whites told Mathers about the songs she was scouting for Beyonce's forthcoming album, Mathers played her “Sandcastles.”

“Midian was like, ‘he's gonna kill me, and he didn't give permission, but I think this is the kind of song Beyonce needs to sing,'?” Berry said with a laugh. “And when she played it, Teresa started to cry. I did this song to heal myself, and I think that's what people are experiencing when they hear it.”

Although Berry was close to landing the biggest placement of his career, he had to keep it secret for more than a year.

As Beyonce worked on the songs for her new album — she received “Sandcastles” in February 2015, Berry said — the songwriter knew he wanted to keep ownership of the record instead of signing a publishing agreement, but that meant making great sacrifices.

“I wanted to own it, and that's a harder road. That meant being homeless and sleeping in cars and garages and studios, and that's what I was willing to do,” said Berry, who now lives in Woodland Hills, Calif. “I knew I couldn't sign a publishing deal knowing what they are — essentially a high-interest loan. Why would I take that kind of deal?”

While he waited, and couch-surfed, Berry placed songs with Nico & Vinz, Shaggy and Mary J. Blige.

And then he got the call he was waiting for: The song he co-wrote landed on the album, and he would share a production credit with Beyonce.

On the final version of the record, which Berry heard ahead of the album's April release, Beyonce wrote a more hopeful ending than the one Berry originally penned.

“And your heart is broken cause I walked away/ Show me your scars and I won't walk away,” Beyonce sang. “And I know I promised that I couldn't stay, baby/ (But) every promise don't work out that way.”

“There was no rebuilding, the relationship was gone. But my relationship wasn't between a husband and a wife,” Berry said of the song's original somber ending. “She's singing about a husband, and there's a different desire to hold on to that relationship.”

Naturally, landing the high-profile placement has greatly affected Berry's stock.

He's now highly in demand, logging studio time with Alicia Keys, Maroon 5, Big Sean, BJ the Chicago Kid and Eminem.

Berry is also working on music for two highly anticipated films, “Birth of a Nation” and the Tupac biopic, “All Eyez on Me”; and he will compete on NBC's upcoming songwriting competition, “Songland.”

“When you get a great opportunity like Beyonce, you've got to leverage that moment,” Berry said. “The industry was already aware of what I'd been doing, but Beyonce opened so many doors for me.”

gerrick.kennedy@tribpub.com