easy  c++ solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    \n    bool check(vector<vector<int>>& image,int r,int c,int k)\n    {\n        int n = r+2;\n        int m = c+2;\n        if(n >= image.size() || m >= image[0].size()) return 0;\n        \n        for(int i = r;i<=n;i++)\n        {\n            for(int j = c;j<=m;j++)\n            {\n                if(i+1<=n && abs(image[i+1][j] - image[i][j]) > k)\n                    return 0;\n                if(j+1<=m && abs(image[i][j+1] - image[i][j]) > k)\n                    return 0;\n                if(i-1 >= r && abs(image[i-1][j] - image[i][j]) > k)\n                    return 0;\n                if(j-1 >= c && abs(image[i][j-1] - image[i][j]) > k)\n                    return 0;\n            }\n        }\n        return 1;\n        \n    }\n    \n    void help(vector<vector<int>>& image,vector<vector<pair<int,int>>> &vt,int k,int r,int c,vector<vector<int>> &vis)\n    {\n        if(r>= image.size() || c>= image[0].size()) return ;\n        int n = r+2;\n        int m = c+2;\n        if(vis[r][c] != -1) return;\n        vis[r][c] = 1;\n        if(check(image,r,c,k))\n        {\n            int avg = 0;\n            for(int i = r;i<=n;i++)\n            {\n                for(int j = c;j<=m;j++)\n                {\n                    avg += image[i][j]; \n                }\n            }\n            avg = avg/9;\n            for(int i = r;i<=n;i++)\n            {\n                for(int j = c;j<=m;j++)\n                {\n                    vt[i][j].first += avg;\n                    vt[i][j].second++;\n                }\n            }\n        }\n        \n        help(image,vt,k,r+1,c,vis);\n        help(image,vt,k,r,c+1,vis);\n        \n        \n    }\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        vector<vector<int>> vist(500,vector<int>(500,-1));\n        vector<vector<pair<int,int>>> vt(image.size(),vector<pair<int,int>>(image[0].size(),{0,0}));\n        vector<vector<int>> ans(image.size(),vector<int>(image[0].size(),0));\n        help(image,vt,threshold,0,0,vist);\n        for(int i = 0;i<image.size();i++)\n        {\n            for(int j = 0;j<image[0].size();j++)\n            {\n                if(vt[i][j].second == 0)\n                    ans[i][j] = image[i][j];\n                else\n                ans[i][j] = vt[i][j].first/vt[i][j].second;\n            }\n        }\n        return ans;\n        \n    }\n};\n```
['C++']