easy understand
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(m*n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(m*n)\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int m = image.size(), n = image[0].size();\n        vector<vector<int>> ans(m, vector<int>(n));\n        vector<vector<int>> counts(m, vector<int>(n));\n        for (int i = 1; i < m - 1; i++) {\n            for (int j = 1; j < n - 1; j++) {\n                int checkResult = checkThreshold(image, threshold, i, j);\n                if (checkResult == -1) {\n                    continue;\n                }\n                fillValues(ans, counts, checkResult, i, j);\n            }\n        }\n\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (counts[i][j] == 0) {\n                    ans[i][j] = image[i][j];\n                    continue;\n                }\n                ans[i][j] /= counts[i][j];\n            }\n        }\n\n        return ans;\n    }\n\nprivate:\n    int checkThreshold(const vector<vector<int>>& image, int threshold, int row, int col) {\n        for (int i = row - 1; i < row + 1; i++) {\n            for (int j = col - 1; j <= col + 1; j++) {\n                if (abs(image[i][j] - image[i + 1][j]) > threshold) {\n                    return -1;\n                }\n            }\n        }\n        for (int j = col - 1; j < col + 1; j++) {\n            for (int i = row - 1; i <= row + 1; i++) {\n                if (abs(image[i][j] - image[i][j + 1]) > threshold) {\n                    return -1;\n                }\n            }\n        }\n\n        int sum = 0;\n        for (int i = row - 1; i <= row + 1; i++) {\n            for (int j = col - 1; j <= col + 1; j++) {\n                sum += image[i][j];\n            }\n        }\n\n        return sum / 9;\n    }\n\n    void fillValues(vector<vector<int>>& ans, vector<vector<int>>& counts, int val, int row, int col) {\n        for (int i = row - 1; i <= row + 1; i++) {\n            for (int j = col - 1; j <= col + 1; j++) {\n                ans[i][j] += val;\n                counts[i][j]++;\n            }\n        }\n    }\n};\n```
['C++']