Simple Brute force
Intuition\n Describe your first thoughts on how to solve this problem. \nJust start doing what the problem saying \n\n# Approach\n Describe your approach to sol
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nJust start doing what the problem saying \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n# Complexity\n- Time complexity:\nO(n*m*9)\nwhere n is the number of rows and m is the number of columns\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n*m)\n\n# Code\n```\nclass Solution {\npublic:\n    // vector<vector<int>>dirs={{1,0},{0,1},{-1,0},{0,-1}};\n    \n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n      int n=image.size() ,m=image[0].size();\n      vector<vector<int>>ans(n,vector<int>(m,-1));\n      vector<vector<int>>count(n,vector<int>(m,1));\n      for(int i=0;i<n;i++){\n        for(int j=0;j<m;j++){\n            if(i+2<n && j+2<m){\n                bool flag=false;\n                int sum=0;\n                for(int k=0;k<3;k++){\n                    for(int l=0;l<3;l++){\n                        int x =i+k;\n                        int y =j+l;\n                        sum += image[x][y];\n                        if(x+1<n&&x+1<=i+2){\n                            if(abs(image[x+1][y]-image[x][y])>threshold){\n                                flag=true;\n                                break;\n                            }\n                        }\n                        if(y+1<m&& y+1<=j+2){\n                            if(abs(image[x][y+1]-image[x][y])>threshold){\n                                flag=true;\n                                break;\n                            }\n                        }\n                    }\n                    if(flag){\n                        break;\n                    }\n                }\n                if(!flag){\n                    for(int k=0;k<3;k++){\n                        for(int l=0;l<3;l++){\n                            int x=i+k;\n                            int y=j+l;\n                            int avg=sum/9;\n                            if(ans[x][y]==-1){\n                                ans[x][y]=avg;\n                            }else{\n                                ans[x][y] +=avg;\n                                count[x][y]++;\n                            }\n                        }\n                    }\n                }\n            }\n        }\n      }  \n\n      for(int i=0;i<n;i++){\n        for(int j=0;j<m;j++){\n            if(ans[i][j]==-1){\n                ans[i][j]=image[i][j];\n            }else{\n                ans[i][j]=ans[i][j]/count[i][j];\n            }\n        }\n      }\n    \n        return ans;\n\n    }\n};\n```
['C++']