Honestly lengthy but easy to understand solution
Intuition\n Describe your first thoughts on how to solve this problem. \nThe question is basically simple but too lengthy\n# Approach\n Describe your approach t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe question is basically simple but too lengthy\n# Approach\n<!-- Describe your approach to solving the problem. -->\nStore the sum of mean intensities and number of region an index lies in. Finally get the mean of these values by dividing the sum of intensities by the number of regions\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n*n):\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n*n)\n# Code\n```\nclass Solution {\npublic:\n    int getMeanIntensity(vector<vector<int>>& image, int x, int y) {\n        int sum = 0;\n        for(int i = x-1; i<=x+1; i++) {\n            for(int j = y-1; j<= y+1; j++) {\n                sum+=image[i][j];\n            }\n        }\n        return sum/9;\n    }\n    \n    void updateRes(int x, int y, vector<vector<pair<int, int>>>& res, int val) {\n        for(int i = x-1; i<=x+1; i++) {\n            for(int j = y-1; j<= y+1; j++) {\n                res[i][j].first += val;\n                res[i][j].second += 1;\n            }\n        }\n    }\n    \n    bool compare(int val, int i, int j, int thres,vector<vector<int>>& image, int cx, int cy) {\n        if(i<0 || j <0 || i==image.size() || j==image[0].size() || i<cx-1 || i>cx+1 || j<cy-1 || j>cy+1) {\n            return true;\n        }\n        return abs(val - image[i][j]) <= thres;\n    }\n    \n    bool belowThreshold(vector<vector<int>>& image, int x, int y, int thres, int cx, int cy) {\n        if(!compare(image[x][y], x-1, y, thres, image, cx, cy)){\n            return false;\n        }\n        if(!compare(image[x][y], x+1, y, thres, image, cx, cy)){\n            return false;\n        }\n        if(!compare(image[x][y], x, y-1, thres, image, cx, cy)){\n            return false;\n        }\n        if(!compare(image[x][y], x, y+1, thres, image, cx, cy)){\n            return false;\n        }\n        return true;\n    }\n    \n    bool isRegion(vector<vector<int>>& image, int x, int y, int thres) {\n        for(int i = x-1; i<=x+1; i++) {\n            for(int j = y-1; j<=y+1; j++) {\n                if(!belowThreshold(image, i, j, thres, x, y)) {\n                    return false;\n                }\n            }\n        }\n        return true;\n    }\n    \n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n = image.size(), m = image[0].size();\n        vector<vector<pair<int, int>>> res(n, vector<pair<int, int>>(m));\n        for(int i = 1; i<n-1; i++) {\n            for(int j = 1; j < m-1; j++) {\n                if(isRegion(image, i, j, threshold)) {\n                    updateRes(i, j, res, getMeanIntensity(image, i, j));\n                }\n            }\n        }\n        \n        \n        for(int i = 0; i<n; i++) {\n            for(int j = 0; j<m; j++) {\n                if(res[i][j].second > 0) {\n                    image[i][j] = res[i][j].first/res[i][j].second;\n                }   \n            }\n        }\n        return image;\n    }\n};\n```
['C++']