Riot's Mastery Grading system works like this: Your rating gets compared to other people of your Region who play the same Champion in the same Role. The higher you score compared to those other players the better your Grade will be.
Everything else about the system is hidden so we have no further information on it. So nobody really knows how to get a high rating. Although there is one fact we do know, winning a game is not required to get a high rating, you can still get S ranks when you lose.
It is also very unlikely (pretty much certain) that kda and cs do not affect the rating in any direct way. That's not to say that getting good kda and cs isn't helpful, but only indirectly so. Not getting an S isn't because your cs is too low, or because you didn't ward enough, or any other such nonsense. You should still focus on getting them, but only because that makes you better at the game, not for your score.
There's a lot of guesswork and ideas on the boards about this system and how it works so never just believe people (myself included) when they tell you what to do to get a higher grade. I've seen a good player with great scores get no higher then a A+, I've seen a poor player get a lucky S, I've seen a good player with really poor kda get a S when losing, I've seen a player get a S because his enemies where dumb. I've yet to find any true pattern that I can prove.
The best you can do is just not worry about the system. We simply don't know how it works yet. Just improve yourself, get better at the game, the better you are the easier it will be to getting a higher grade. Plus, its always possible there's an element of luck to it.
So, good luck.