It is certain that man must utterly despair of his own ability before he is prepared to receive the grace of Christ. (Martin Luther, Heidelberg Disputation of 1518, Thesis 18)
He went into the monastery in order to find peace. He thought that if he gave his whole life and every moment in praise and prayer to God that maybe he would have some peace. He had none. Instead, he despaired. The more he tried to earn God's grace, the further he felt he was from God. Then, he despaired completely and in his utter despair, he was finally able to see the truth that was there all along: "For it is by grace you have been saved, through faith—and this is not from yourselves, it is the gift of God—not by works, so that no one can boast." (Eph 2:8–9)
The Heidelberg Disputation was written by Martin Luther the year after he nailed the 95 Thesis on the church door in Wittenberg. He was invited to defend those thesis, especially on indulgences, to fellow monks of the Augustinian Order, and he used the opportunity to state clearly what Scripture reveals: we cannot save ourselves, but only in Christ are we saved. To think anything else diminishes the work of Christ.
To learn more about the Heidelberg Disputation and to read the other Theses (which are succinct and clear), go here: https://shorturl.at/abcvF