Implementation | Brute Force | C++
\nclass Solution {\npublic:\n    \n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int th) {\n        int m = image.size();\n        int n = imag
```\nclass Solution {\npublic:\n    \n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int th) {\n        int m = image.size();\n        int n = image[0].size();\n        vector<vector<int>> result(m, vector<int>(n, 0)), cnt(m, vector<int>(n, 0));\n        vector<int> dx={0,1,0,-1}, // movements\n                    dy={1,0,-1,0}; \n        for (int i = 0; i < m-2; i++) { //Len of 3 requires 3 empty spaces\n            for (int j = 0; j < n-2; j++) {  //Width of 3 requires 3 empty spaces\n                int sum = 0;\n                int no=0;\n                for(int x=i;x<i+3;x++) { //Len of 3 formation (square)\n                    for(int y=j;y<j+3;y++) { //Width of 3\n                        bool ad=0; //To add only once\n                        for(int z=0;z<4;z++) { //For all the adjacent directions\n                            int nx=x+dx[z],ny=y+dy[z];\n                            if(nx>=i and nx<i+3 and ny>=j and ny<j+3) { //Checking if it is valid and in the square\n                                if(abs(image[nx][ny]-image[x][y])<=th) { //Checking if it is less than threshold\n                                    if(!ad) //If added once, no need to add again else repetation will result in WA\n                                    sum+=image[x][y],ad=1; //Mark as added by ad=1\n                                }\n                                else {\n                                    no=1; //If outside threshold, not possible, break\n                                    break;\n                                }\n                            }\n                        }\n                    }\n                }\n                if(no) continue;\n                sum/=9; //Square of 3*3 = 9\n                for(int x=i;x<i+3;x++) {\n                    for(int y=j;y<j+3;y++) {\n                        result[x][y]+=sum; //Add the sum\n                        cnt[x][y]++;  //Count to get the average in the end\n                    }\n                }\n            }\n        }\n        for(int i=0;i<m;i++) { //For answer\n            for(int j=0;j<n;j++) {\n                // cout<<result[i][j]<<" "<<cnt[i][j]<<" "<<i<<" "<<j<<endl;\n                if(cnt[i][j]==0)  \n                    result[i][j]=image[i][j];\n                else\n                    result[i][j]/=cnt[i][j]; \n            }\n        }\n        return result;\n    }\n};\n```
['C']