public class Solution { int total = 0; public int movingCount(int threshold, int rows, int cols) { boolean[][] map = new boolean[rows][cols]; func(threshold,rows,cols,0,0,map);

	return total;
}

public void func(int threshold, int rows, int cols,int x,int y,boolean[][] map) {
	if(!map[x][y]) {
		if(count(x) + count(y) <= threshold) {
			total++;
			map[x][y] = true;
			if(x > 0) func(threshold,rows,cols,x-1,y,map);
			if(y > 0) func(threshold,rows,cols,x,y-1,map);
			if(x < rows-1) func(threshold,rows,cols,x+1,y,map);
			if(y < cols-1) func(threshold,rows,cols,x,y+1,map);
		}else map[x][y] = true;
	}else return;
	
	return;
}

public int count(int num) {
	int n = 0;
	while(num >= 10) {
		n += num % 10;
		num /= 10;
	}
	n += num;
	
	return n;
}

}