Brute force approach
Code\n\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n = image.size();\n        int m
# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n = image.size();\n        int m = image[0].size();\n        vector<vector<int>> totalAverage(n, vector<int>(m, 0));\n        vector<vector<int>> totalCount(n, vector<int>(m, 0));\n        int count = 1;\n        for(int i = 0; i < n; i++) {\n            for(int j = 0; j < m; j++) {\n                int newI = i + 2;\n                int newJ = j + 2;\n                if(newI < n && newJ < m) {\n                    long long total = 0;\n                    bool flag = true;\n                    for(int k = i; k <= newI; k++) {\n                        int maximumDiff = 0;\n                        for(int l = j; l <= newJ; l++) {\n                            if(k - 1 >= i) {\n                                maximumDiff = max(maximumDiff, abs(image[k][l] - image[k-1][l]));\n                            }\n                            if(k + 1 <= newI) {\n                                maximumDiff = max(maximumDiff, abs(image[k][l] - image[k+1][l]));\n                            }\n                            if(l + 1 <= newJ) {\n                                maximumDiff = max(maximumDiff, abs(image[k][l] - image[k][l + 1]));\n                            }\n                            if(l - 1 >= j) {\n                                maximumDiff = max(maximumDiff, abs(image[k][l] - image[k][l - 1]));\n                            }\n                            if(maximumDiff > threshold) {\n                                flag = false;\n                            }\n                            total += image[k][l];\n                        }\n                    } \n                    if(flag) {\n                        double av = (double)((double) total / (double) 9);\n                        int avR = floor(av);\n                        for(int k = i; k <= newI; k++) {\n                            for(int l = j; l <= newJ; l++) {\n                                totalAverage[k][l] += avR;\n                                totalCount[k][l]++;\n                            }\n                        } \n                        \n                    }\n                }\n            }\n        }\n        vector<vector<int>> ans(n, vector<int>(m, 0));\n        for(int i = 0; i < n; i++) {\n            for(int j = 0; j < m; j++) {\n                if(totalCount[i][j] > 0) {\n                    double av = (double)((double) totalAverage[i][j] / (double) totalCount[i][j]);\n                    int avR = floor(av);\n                    ans[i][j] = avR;\n                } else {\n                    ans[i][j] = image[i][j];\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']