Super Easy || C++
Code\n\nclass Solution \n{\npublic:\n    bool valid(int x, int y, int m, int n, int rows, int cols)\n    {\n        return (x>=m && x<min(m+3,rows) && y>=n && y
# Code\n```\nclass Solution \n{\npublic:\n    bool valid(int x, int y, int m, int n, int rows, int cols)\n    {\n        return (x>=m && x<min(m+3,rows) && y>=n && y<min(n+3,cols));\n    }\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) \n    {\n        int m=image.size(), n=image[0].size();\n        vector<vector<int>> ans(m, vector<int> (n,0));\n        vector<vector<vector<int>>> v(m, vector<vector<int>> (n, vector<int> (0)));\n        vector<pair<int,int>> directions={{-1,0},{0,-1},{1,0},{0,1}};\n        for(int x=0; x<=m-3; x++)\n        {\n            for(int y=0; y<=n-3; y++)\n            {\n                int region=1;\n                int sum=0;\n                for(int dx=x; dx<=x+2; dx++)\n                {\n                    for(int dy=y; dy<=y+2; dy++)\n                    {\n                        sum+=image[dx][dy];\n                        for(auto it: directions)\n                        {\n                            int dr=dx+it.first, dc=dy+it.second;\n                            if(valid(dr,dc,x,y,m,n))\n                            {\n                                if(abs(image[dx][dy]-image[dr][dc])>threshold)\n                                {\n                                    region=0;\n                                    break;\n                                }\n                            }\n                        }\n                        if(!region) break;\n                    }\n                    if(!region) break;\n                }\n                if(region)\n                {\n                    int avg=sum/9;\n                    for(int dx=x; dx<=x+2; dx++)\n                    {\n                        for(int dy=y; dy<=y+2; dy++)\n                        {\n                            v[dx][dy].push_back(avg);\n                        }\n                    }\n                }\n            }\n        }\n        for(int x=0; x<m; x++)\n        {\n            for(int y=0; y<n; y++)\n            {\n                auto t=v[x][y];\n                int sz=t.size();\n                if(sz)\n                {\n                    int sum=accumulate(t.begin(),t.end(),0);\n                    ans[x][y]=sum/sz;\n                }\n                else ans[x][y]=image[x][y];\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']