Simple and easy to understand solution
\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        m, n = len(image), len(image[0])\n        grid 
```\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        m, n = len(image), len(image[0])\n        grid = [[[0, 0] for _ in range(n)] for _ in range(m)]\n        for i, j in product(range(m-2), range(n-2)):\n            s = 0\n            for ii, jj in product(range(i, i+3), range(j, j+3)):\n                if (ii < i+2 and abs(image[ii+1][jj] - image[ii][jj]) > threshold or \n                    jj < j+2 and abs(image[ii][jj+1] - image[ii][jj]) > threshold):\n                    break\n                s += image[ii][jj]\n            else:\n                s //= 9\n                for ii, jj in product(range(i, i+3), range(j, j+3)):\n                    grid[ii][jj][0] += s\n                    grid[ii][jj][1] += 1\n\n        for i, j in product(range(m), range(n)):\n            if grid[i][j][1]:\n                image[i][j] = grid[i][j][0] // grid[i][j][1]\n        return image\n```
['Python3']