Simple Easy C++ Bruteforce Solution
Intuition\n Describe your first thoughts on how to solve this problem. \njust follow what is asked in question \n# Approach\n Describe your approach to solving 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\njust follow what is asked in question \n# Approach\n<!-- Describe your approach to solving the problem. -->\n by using loops in solve function for every element we find its all possible region and calculate averagae  \n# Complexity\n- Time complexity: O(n*m*9*9) in worst case\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  \n    \n    int solve(int i,int j,vector<vector<int>>& nums,int k){\n        int x=max(i-2,0);\n        int y=max(j-2,0);\n         int n=nums.size();\n        int m=nums[0].size();\n        int avg=0;\n        int count=0;\n        while(x<=i){\n             if(x+2>=n) {\n                    break;\n                }\n            int t=y;\n            while(t<=j){\n               \n                if(t+2>=m){\n                   break;  \n                }\n                bool isval=1;\n                long long sum=0;\n                for(int a=0;a<3;a++){\n                    for(int b=0;b<3;b++){\n                        int ii=x+a;\n                        int jj=t+b;\n                       \n                        int d=-1;\n                        int l=-1;\n                            if(ii!=x+2) d=nums[ii+1][jj];\n                        if(jj!=t+2) l=nums[ii][jj+1];\n                        \n                        if(d!=-1 && abs(nums[ii][jj]-d)>k){\n                           isval=0; \n                            break;\n                        }\n                            \n                         if(l!=-1 && abs(nums[ii][jj]-l)>k){\n                           isval=0; \n                             break;\n                        }\n                        sum+=nums[ii][jj];\n                    }\n                }\n                if(isval){\n                    avg+=sum/9;\n                    count++;\n}\n          t++;  }\n            x++;\n        }\n        \n        if(count==0) return nums[i][j];\n        return avg/count;\n    }\n    vector<vector<int>> resultGrid(vector<vector<int>>& nums, int k) {\n        \n        int n=nums.size();\n        int m=nums[0].size();\n        vector<vector<int>> ans(n,vector<int>(m));\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n              ans[i][j]=solve(i,j,nums,k); \n            }\n        }\n        return ans;\n    }\n};\n```
['C++']