#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param threshold int整型 
# @param rows int整型 
# @param cols int整型 
# @return int整型
#
class Solution:
    def movingCount(self , threshold: int, rows: int, cols: int) -> int:
        # write code here
        grid = [[0 for i in range(cols)] for j in range(rows)]
        start=[[0,0]]
        while start:
            p1,p2 = start.pop(0)
            if grid[p1][p2] == -1:
                continue
            cumsum = sum([ int(i) for i in str(p1)+str(p2)])
            if cumsum<=threshold:
                grid[p1][p2] = -1
                for i,j in [(-1,0),(1,0),(0,-1),(0,1)]:
                    _p1,_p2 = p1+i,p2+j
                    if _p1<0 or _p1>=rows or _p2<0 or _p2>=cols:
                        continue
                    else:
                        start.append([p1+i,p2+j])
            print(start)
        ans=0
        for i in range(rows):
            for j in range(cols):
                if grid[i][j]==-1:
                    ans+=1
        return ans