Brute Force Python Solution using Product
O(n^2 * 9) space and runtime so O(n^2) for both\n\n# Code\n\nclass Solution:\n    def resultGrid(self, image: List[List[int]], t: int) -> List[List[int]]:\n    
O(n^2 * 9) space and runtime so O(n^2) for both\n\n# Code\n```\nclass Solution:\n    def resultGrid(self, image: List[List[int]], t: int) -> List[List[int]]:\n        rows = len(image)\n        cols = len(image[0])\n        res = defaultdict(list)\n\n        for i in range(rows -2):\n            for j in range(cols -2):\n                total = 0\n                for x,y in product([0,1,2],[0,1,2]):\n                    up = i + x - 1 >= i\n                    down = i + x + 1 <= i +2\n                    left = j + y -1 >= j\n                    right = j + y +1 <= j+2\n                    total += image[i +x][j+ y]\n                    if up and abs(image[i +x][j+ y] - image[i +x -1][j+ y]) > t: break\n                    if down and abs(image[i +x][j+ y] - image[i +x +1][j+ y]) > t: break\n                    if left and abs(image[i +x][j+ y] - image[i +x][j+ y-1]) > t: break\n                    if right and abs(image[i +x][j+ y] - image[i +x][j+ y + 1]) > t: break\n                else:\n                    for x,y in product([0,1,2],[0,1,2]):\n                        res[(i+x,j+y)].append(total//9)\n                \n        \n        ans = [[0] * cols for _ in range(rows)]\n        for i in range(rows):\n            for j in range(cols):\n                if len(res[(i,j)]) == 0: ans[i][j] = image[i][j]\n                else: ans[i][j] = sum(res[(i,j)]) // len(res[(i,j)])\n        return ans\n\n        \n```
['Python3']