If input is outside range

how to validate M and N should be between 1 and 10 ?
and if I give input outside this range then what to do ?

The value of M and N will be in between 1-10 for all the test cases that the system will give to check your code. You are free to give input outside this range, its just that then the program will take more time to execute.