Hi, I hope is not too late Look at this line: if(map[O-1][M][N]!='#'&&O-1>=0&&!vis[O-1][M][N]) doIt(O-1,M,N,dep); The thing is that you are evaluating map[O-1][M][N] before knowing whether O-1>=0 or not, that's why you get runtime error, just switch the conditions :D Remove your code...