This is my ide.
In this two test cases get passed and rest shows time limit error.
Regarding rat in a maze problem
In the question, it is mentioned that the size of M and N is 1000, so change your array size accordingly and then let me know if it is submitted or not.