Python | Bruetforce | O(mn)
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        
# 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        sumMatrix = [[0]*n for _ in range(m)]\n        cntMatrix = [[0]*n for _ in range(m)]\n        def checkRegion(i,j):\n            for p in range(i,i+3):\n                for q in range(j,j+3):\n                    if p+1<=i+2 and p+1<m and abs(image[p][q]-image[p+1][q]) > threshold: return False \n                    if q+1<=j+2 and q+1<n and abs(image[p][q]-image[p][q+1]) > threshold: return False \n            return True\n        for i in range(m-2):\n            for j in range(n-2):\n                if checkRegion(i,j):\n                    sumRegion = sum([image[p][q] for p in range(i,i+3) for q in range(j,j+3)])\n                    for p in range(i,i+3):\n                        for q in range(j,j+3):\n                            cntMatrix[p][q] += 1\n                            sumMatrix[p][q] += sumRegion//9 \n        print(cntMatrix)\n        print(sumMatrix)\n        res = [[0]*n for _ in range(m)]\n        for i in range(m):\n            for j in range(n):\n                res[i][j] = sumMatrix[i][j]//cntMatrix[i][j] if cntMatrix[i][j] else image[i][j]\n        return res\n\n        \n```
['Python3']