Brute force. Dumb but clear
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)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nbool is_region(vector<vector<int>> const& image, int i, int j, int threshold)\n{\n    return abs(image[i - 1][j - 1] - image[i - 1][j]) <= threshold\n        && abs(image[i - 1][j] - image[i - 1][j + 1]) <= threshold\n        && abs(image[i][j - 1] - image[i][j]) <= threshold\n        && abs(image[i][j] - image[i][j + 1]) <= threshold\n        && abs(image[i + 1][j - 1] - image[i + 1][j]) <= threshold\n        && abs(image[i + 1][j] - image[i + 1][j + 1]) <= threshold\n        && abs(image[i - 1][j - 1] - image[i][ j - 1]) <= threshold\n        && abs(image[i - 1][j] - image[i][j]) <= threshold\n        && abs(image[i - 1][j + 1] - image[i][j + 1]) <= threshold\n        && abs(image[i][j - 1] - image[i + 1][j - 1]) <= threshold\n        && abs(image[i][j] - image[i + 1][j]) <= threshold\n        && abs(image[i][j + 1] - image[i + 1][j + 1]) <= threshold;\n}\n\nauto sum_of_region(vector<vector<int>> const& image, int i, int j)\n{\n    return image[i - 1][j - 1] + image[i - 1][j] + image[i - 1][j + 1]\n         + image[i][j - 1] + image[i][j] + image[i][j + 1]\n         + image[i + 1][j - 1] + image[i + 1][j] + image[i + 1][j + 1];\n}\n\nvoid update(pair<int, int>& pixel_info, int avg)\n{\n    pixel_info.first += avg;\n    pixel_info.second += 1;\n}\n\nint average(pair<int, int> const& pixel_info)\n{\n    return pixel_info.first / pixel_info.second;\n}\n\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int const m = image.size();\n        int const n = image[0].size();\n\n        auto pixel_info = vector<vector<pair<int, int>>>(m, vector<pair<int, int>>(n, pair(0, 0)));\n\n        for (auto [i, row] = pair(1, m - 1); i < row; ++i) {\n            for (auto [j, col] = pair(1, n - 1); j < col; ++j) {\n                if (is_region(image, i, j, threshold)) {\n                    auto avg = sum_of_region(image, i, j) / 9;\n                    update(pixel_info[i - 1][j - 1], avg);\n                    update(pixel_info[i - 1][j], avg);\n                    update(pixel_info[i - 1][j + 1], avg);\n                    update(pixel_info[i][j - 1], avg);\n                    update(pixel_info[i][j], avg);\n                    update(pixel_info[i][j + 1], avg);\n                    update(pixel_info[i + 1][j - 1], avg);\n                    update(pixel_info[i + 1][j], avg);\n                    update(pixel_info[i + 1][j + 1], avg);\n                }\n            }\n        }\n\n        auto ans = image;\n\n        for (auto i = 0; i < m; ++i) {\n            for (auto j = 0; j < n; ++j) {\n                if (pixel_info[i][j].second > 0) {\n                    ans[i][j] = average(pixel_info[i][j]);\n                }\n            }\n        }\n\n        return ans;\n    }\n};\n```
['C++']