Simple decomposed python3 solution
Complexity\n- Time complexity: O(m \cdot n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(m \cdot n) for answer and O(m) for calculate\n 
# Complexity\n- Time complexity: $$O(m \\cdot n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m \\cdot n)$$ for answer and $$O(m)$$ for calculate\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` python3 []\nfrom functools import lru_cache\n\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        m = len(image)\n        n = len(image[0])\n        \n        @lru_cache(maxsize=5 * m)\n        def get_average_by_region(i, j):  # top left corner\n            if not (\n                0 <= i < m - 2\n                and 0 <= j < n - 2\n            ):\n                return None\n            \n            # checking\n            for x in range(3):\n                for y in range(2):\n                    if (\n                        abs(image[i + x][j + y] - image[i + x][j + y + 1]) > threshold\n                        or abs(image[i + y][j + x] - image[i + y + 1][j + x]) > threshold\n                    ):\n                        return None\n\n            # calculate\n            return sum(\n                image[i + di][j + dj]\n                for di in range(3)\n                for dj in range(3)\n            ) // 9\n\n        def get_average_by_pixel(i, j):\n            result = []\n            for di in range(-2, 1):\n                for dj in range(-2, 1):\n                    current = get_average_by_region(i + di, j + dj)\n                    if current is not None:\n                        result.append(current)\n\n            if len(result) == 0:\n                return image[i][j]\n            return sum(result) // len(result) \n\n        return [\n            [\n                get_average_by_pixel(i, j)\n                for j in range(n)\n            ]\n            for i in range(m)\n        ]\n```
['Matrix', 'Simulation', 'Python3']