C++ - Brute force
\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int r=image.size(),c=image[0].size();\n   
```\nclass Solution {\npublic:\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int r=image.size(),c=image[0].size();\n        \n        vector<vector<int>> maps(r,vector<int>(c,-1));\n        \n        for(int i=1;i<r-1;i++){\n            for (int j=1;j<c-1;j++){\n                if (abs(image[i][j]-image[i][j+1])<=threshold && abs(image[i][j]-image[i][j-1])<=threshold && abs(image[i][j]-image[i+1][j])<=threshold && abs(image[i][j]-image[i-1][j])<=threshold && abs(image[i-1][j-1]-image[i-1][j])<=threshold && abs(image[i-1][j-1]-image[i][j-1])<=threshold && abs(image[i+1][j+1]-image[i+1][j])<=threshold && abs(image[i+1][j+1]-image[i][j+1])<=threshold && abs(image[i-1][j+1]-image[i-1][j])<=threshold && abs(image[i-1][j+1]-image[i][j+1])<=threshold && abs(image[i+1][j-1]-image[i+1][j])<=threshold && abs(image[i+1][j-1]-image[i][j-1])<=threshold){\n                    maps[i][j]=0;\n                    for(int k=i-1;k<i+2;k++){\n                        for (int l=j-1;l<j+2;l++){\n                            maps[i][j]+=image[k][l];\n                        }\n                    }\n                    maps[i][j] /=9;\n                }\n            }\n        }\n        \n        vector<vector<int>> ans(r,vector<int>(c,0));\n        int rc=0,rt=0;\n        for (int i=0;i<r;i++){\n            for (int j=0;j<c;j++){\n                rc=0,rt=0;\n                for(int k=i-1;k<i+2;k++){\n                    for (int l=j-1;l<j+2;l++){\n                        if (k>=0 && l>=0 && k<r && l<c && maps[k][l]!=-1){\n                            rc++;\n                            rt+=maps[k][l];\n                        }\n                    }\n                }\n                if (rc>0) ans[i][j]=rt/rc;\n                else ans[i][j]=image[i][j];\n            }\n        }\n        return ans;\n    }\n};\n```
[]