Easy C++ Solution || Simple Logic
\n# Complexity\n- Time complexity:O(mn)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(mn)\n Add your space complexity here, e.g. O(n) \n\n
\n# Complexity\n- Time complexity:O(m*n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(m*n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int isRegion(int i,int j,int threshold,vector<vector<int>> &image)\n    {\n        int sum=0;\n        for(int k=0;k<3;k++)\n        {\n            if(abs(image[i+k][j+1]-image[i+k][j])>threshold || abs(image[i+k][j+1]-image[i+k][j+2])>threshold)\n                return -1;\n            else\n                sum+=image[i+k][j]+image[i+k][j+2];\n            sum+=image[i+k][j+1];\n        }\n        for(int l=0;l<3;l++)\n        {\n            if(abs(image[i+1][j+l]-image[i][j+l])>threshold || abs(image[i+1][j+l]-image[i+2][j+l])>threshold)\n                return -1;\n        }\n        return sum/9;\n    }\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int m=image.size();\n        int n=image[0].size();\n        vector<vector<int>> v(m,vector<int>(n,0));\n        vector<vector<int>> count(m,vector<int>(n,0));\n        for(int i=0;i<m-2;i++)\n        {\n            for(int j=0;j<n-2;j++)\n            {\n                int sum=isRegion(i,j,threshold,image);\n                if(sum!=-1)\n                {\n                    for(int k=0;k<3;k++)\n                    {\n                        for(int l=0;l<3;l++)\n                        {\n                            v[i+k][j+l]+=sum;\n                            count[i+k][j+l]+=1;\n                        }\n                    }\n                }\n            }\n        }\n        for(int i=0;i<m;i++)\n        {\n            for(int j=0;j<n;j++)\n            {\n                if(count[i][j]>0)\n                {\n                    v[i][j]=v[i][j]/count[i][j];\n                }\n                else\n                {\n                    v[i][j]=image[i][j];\n                }\n            }\n        }\n        return v;\n    }\n};\n```
['C++']