The Gemara in Masechet Sota teaches that it is even okay for an individual to be involved with Torah not for the sake of Heaven. For even if he does this with the wrong intent, it will lead to the correct intent.
This idea is learned from Balak. He offered forty-two sacrifices with the intent of finding favor with G-d, so that he could curse the Jewish people. You can’t have any worse intent than this. Nevertheless, he was blessed to have Ruth the Moabitess, descend from him. King David and King Solomon descended from her, and the Mashiach, will also descend from this line.
Shlomo Hamelech rectified the insincere nature of Balak’s sacrifices, by offering one thousand sacrifices. He did this on the day of the dedication of the Temple that he built. There is even a Midrash that says that Ruth was still alive when Shlomo reigned. He even had a throne set up for her, near his own throne.
Sometimes people have difficulties in motivating themselves to do Mitzvot. Their Yeitzer Hara may play tricks on them, that takes away their enthusiasm. We need to push ourselves to go to Shule to pray with a Minyan. We need to get ourselves involved with Torah study and classes. Even if it does not start out for the right reasons, it eventually will lead to the right reasons.
Surprisingly, we learn this from Balak. He was far from sincere and was still rewarded. This is what is meant by the saying:
מתוך שלא לשמה בא לשמה.
That which began not for the sake of Heaven, will be for the sake of Heaven.