Beats 99.79% users.... # QualityCode
\n\n# Code\n\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        import numpy as np\n        import 
\n\n# Code\n```\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        import numpy as np\n        import scipy\n        img = np.array(image)\n        m, n = img.shape\n        dx = np.abs(img[1:] - img[:-1])\n        dy = np.abs(img[:, 1:] - img[:, :-1])\n        dall = []\n        for i in range(3):\n            dall += [dx[1:, i:n - 2 + i], dx[:-1, i:n - 2 + i], dy[i:m - 2 + i, 1:], dy[i:m - 2 + i, :-1]]\n        mask = np.max(dall, axis=0) <= threshold\n        avg = (scipy.signal.convolve2d(img, np.ones((3, 3)) / 9, \'valid\') + 1e-8).astype(int)\n        eff = mask * avg\n        ans =  np.zeros_like(img)\n        mans = np.zeros_like(img)\n        for i in range(3):\n            for j in range(3):\n                ans[i:m - 2 + i, j:n - 2 + j] += eff\n                mans[i:m - 2 + i, j:n - 2 + j] += mask\n        result = ans / mans\n        na = np.isnan(result)\n        result[na] = img[na]\n        return (result + 1e-8).astype(int)\n```
['Python', 'Python3']