why my code for RECTQUER is giving TLE while same algorithm in c++ is executing in 0.98 sec??
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class rectq{
static int mat[][][]=new int [301][301][11];
public static void main (String args[])throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int n,r,c,k;
n=Integer.parseInt(in.readLine());
String str;
String REGEX = " ";
for(r=1;r<=n;r++)
{
str=in.readLine();
Pattern pp = Pattern.compile(REGEX);
String[] item = pp.split(str);
for(c=1;c<=n;c++)
{ mat[r][c][0]=Integer.parseInt(item[c-1]);
mat[r][c][mat[r][c][0]]=mat[r][c-1][mat[r][c][0]]+1;
for(k=1;k<=10;k++)
{ if(k!=mat[r][c][0])
mat[r][c][k]=mat[r][c-1][k];
}
}
}
int q;
q=Integer.parseInt(in.readLine());
String str1;
while(q>0){
str1=in.readLine();
int x1,y1,x2,y2,count=0;
Pattern p = Pattern.compile(REGEX);
String[] item = p.split(str1);
int check[]=new int[11];
x1=Integer.parseInt(item[0]);
y1=Integer.parseInt(item[1]);
x2=Integer.parseInt(item[2]);
y2=Integer.parseInt(item[3]);
for(r=x1;r<=x2;r++)
{
for(k=1;k<=10;k++)
{
if(mat[r][y2][k]-mat[r][y1-1][k]>0)
{ if(check[k]==0)
{
check[k]=1;
count++;}
if(count==10)
break;
}
}
if(count==10)
break;
}
System.out.println(count);
q--;
}
}
}