Rover Control

import java.io.*;
import java.util.*;
public class Rover {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
int arr[][]=new int[n][n];
String cmds[]=new String[m];
int i,j,count=0,rpos=0,cpos=0;
in.nextLine();
for(i=0;i<m;i++)
cmds[i] = in.nextLine();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
arr[i][j]=count++;
/*Finding position of robot after executing commands*/
for(i = 0;i<cmds.length;i++){
switch(cmds[i]){
case "up":
case "UP":
if(rpos!=0)
rpos--;
break;
case "down":
case "DOWN":
rpos++;
break;
case "left":
case "LEFT":
if(cpos!=0)
cpos--;
break;
case "right":
case "RIGHT":
cpos++;
break;
}
}
System.out.print(arr[rpos][cpos]); //Displaying element
    }
}