Easy solution [C++]
\n# Code\n\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n = image.size(), m = image[
\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n = image.size(), m = image[0].size();\n        vector<vector<vector<int>>> v(n, vector<vector<int>>(m));\n        vector<vector<int>> res(n, vector<int>(m));\n\n        for (int i = 0; i < n - 2; i++)\n        {\n\n            for (int j = 0; j < m - 2; j++)\n            {\n                int temp_sum = 0;\n                bool excluded = false;\n                for (int k = i; k < i + 3 && k < n; k++)\n                {\n                    for (int l = j; l < j + 3 && l < m; l++)\n                    {\n                        if (!(((k == i) || (abs(image[k - 1][l] - image[k][l]) <= threshold)) && ((k == i + 2) || (abs(image[k + 1][l] - image[k][l]) <= threshold)) && ((l == j) || (abs(image[k][l - 1] - image[k][l]) <= threshold)) && ((l == j + 2) || (abs(image[k][l + 1] - image[k][l]) <= threshold))))\n                        {\n                            excluded = true;\n                        }\n                        else\n                        {\n                            temp_sum += image[k][l];\n                            ;\n                        }\n                    }\n                }\n                if (!excluded)\n                {\n                    for (int k = i; k < i + 3 && k < n; k++)\n                    {\n                        for (int l = j; l < j + 3 && l < m; l++)\n                        {\n                            v[k][l].push_back(temp_sum / 9);\n                        }\n                    }\n                }\n            }\n        }\n        for (int i = 0; i < n; i++)\n        {\n            for (int j = 0; j < m; j++)\n            {\n                int temp_sum = 0;\n                for (int x : v[i][j])\n                {\n                    temp_sum += x;\n                }\n                res[i][j] = v[i][j].size() == 0 ? image[i][j] : temp_sum / v[i][j].size();\n            }\n        }\n        return res;\n    }\n};\n```
['C++']