Just do what it said.
Intuition\nThe way to check if it\'s valid.\n\n\n\n# Code\n\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]
# Intuition\nThe way to check if it\'s valid.\n![\u87A2\u5E55\u64F7\u53D6\u756B\u9762 (676).png](https://assets.leetcode.com/users/images/77b3c04f-c011-4916-84c2-16b12aa8b560_1707019296.3045928.png)\n\n\n# Code\n```\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        m, n = len(image), len(image[0])\n        res = [[0 for _ in range(n)] for _ in range(m)]\n        cnt = [[0 for _ in range(n)] for _ in range(m)]\n        def isValid(x, y):\n            for dx in [-1, 1]:\n                for dy in [-1, 1]:\n                    if not (abs(image[x][y] - image[x+dx][y]) <= threshold and abs(image[x][y] - image[x][y+dy]) <= threshold):\n                        return False\n            if not (abs(image[x-1][y-1] - image[x-1][y]) <= threshold and abs(image[x-1][y-1] - image[x][y-1]) <= threshold):\n                return False\n            if not (abs(image[x-1][y+1] - image[x-1][y]) <= threshold and abs(image[x-1][y+1] - image[x][y+1]) <= threshold):\n                return False\n            if not (abs(image[x+1][y-1] - image[x+1][y]) <= threshold and abs(image[x+1][y-1] - image[x][y-1]) <= threshold):\n                return False\n            if not (abs(image[x+1][y+1] - image[x+1][y]) <= threshold and abs(image[x+1][y+1] - image[x][y+1]) <= threshold):\n                return False\n            return True\n            \n        for i in range(1, m-1):\n            for j in range(1, n-1):\n                if isValid(i, j):\n                    val = 0\n                    for dx in [-1, 0, 1]:\n                        for dy in [-1, 0, 1]:\n                            cnt[i+dx][j+dy] += 1\n                            val += image[i+dx][j+dy]\n                    for dx in [-1, 0, 1]:\n                        for dy in [-1, 0, 1]:\n                            res[i+dx][j+dy] += val // 9\n                \n                \n        for i in range(m):\n            for j in range(n):\n                if cnt[i][j] == 0:\n                    res[i][j] = image[i][j]\n                else:\n                    res[i][j] //= cnt[i][j]\n        \n        return res\n```
['Python3']