class Solution {
public:
    int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
    //上下左右
    int cal(int a)
    {
        if(a<10)
        {
            return a;
        }else if(a>=10&&a<100)
        {
            return a/10+a%10;
        }
        else{
            return 1;
        }
    }
    int res=0;
    void dfs(int threshold,int i,int j,int rows,int cols,vector<vector<bool>>& vv)
    {
        if(i<0||i>=rows||j>=cols||j<0)
        {
            return ;
        }
        if(threshold<cal(i)+cal(j)||vv[i][j]==false)
        {
            return ;
        }
        res+=1;
        vv[i][j]=false;
        for(auto & k : dir)
        {
            dfs(threshold,i+k[0],j+k[1],rows,cols,vv);
        }
    }
    int movingCount(int threshold, int rows, int cols) {
        vector<vector<bool>> vv(rows,vector<bool>(cols,true)) ;
        dfs(threshold,0,0,rows,cols,vv);
        return res;
    }
};