Easy Rust and 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. \nI implemented it as ins
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nI implemented it as instructed in the problem.\n\n# Complexity\n- Time complexity: $$O(n*m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n*m)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n## Rust\n```Rust\nimpl Solution {\n    fn is_region(mat: &Vec<Vec<i32>>, row: usize, col: usize, threshold: i32) -> bool {\n        for i in row..row+3 {\n            for j in col..col+3 {\n                if i > row && i32::abs(mat[i][j] - mat[i - 1][j]) > threshold {\n                    return false;\n                }\n                if j > col && i32::abs(mat[i][j] - mat[i][j - 1]) > threshold {\n                    return false;\n                }\n            }\n        }\n\n        true\n    }\n\n    fn average(mat: &Vec<Vec<i32>>, row: usize, col: usize) -> i32 {\n        let mut sum: i32 = 0;\n        for i in row..row+3 {\n            for j in col..col+3 {\n                sum += mat[i][j];\n            }\n        }\n\n        sum / 9\n    }\n\n    fn add(mat: &mut Vec<Vec<i32>>, row: usize, col: usize, val: i32) {\n        for i in row..row+3 {\n            for j in col..col+3 {\n                mat[i][j] += val;\n            }\n        }\n    }\n\n    pub fn result_grid(image: Vec<Vec<i32>>, threshold: i32) -> Vec<Vec<i32>> {\n        let N = image.len();\n        let M = image[0].len();\n\n        let mut res: Vec<Vec<i32>> = vec![vec![0; M]; N];\n        let mut cnt: Vec<Vec<i32>> = vec![vec![0; M]; N];\n        for i in 0..=(N-3) {\n            for j in 0..=(M-3) {\n                if !Solution::is_region(&image, i, j, threshold) {\n                    continue;\n                }\n\n                let avg = Solution::average(&image, i, j);\n                Solution::add(&mut res, i, j, avg);\n                Solution::add(&mut cnt, i, j, 1);\n            }\n        }\n\n        for i in 0..N {\n            for j in 0..M {\n                res[i][j] = \n                    if cnt[i][j] > 0 {\n                        res[i][j] / cnt[i][j]\n                    } else {\n                        image[i][j]\n                    };\n            }\n        }\n\n        res\n    }\n}\n```\n\n## C++\n```C++\nclass Solution {\npublic:\n    bool isRegion(const vector<vector<int>>& image, int row, int col, int threshold) {\n        for (int i = row; i < row + 3; i++) {\n            for (int j = col; j < col + 3; j++) {\n                if (i > row && abs(image[i][j] - image[i - 1][j]) > threshold)\n                    return false;\n                if (j > col && abs(image[i][j] - image[i][j - 1]) > threshold)\n                    return false;\n            }\n        }\n        return true;\n    }\n\n    int average(const vector<vector<int>>& image, int row, int col) {\n        int sum = 0;\n        for (int i = row; i < row + 3; i++) {\n            for (int j = col; j < col + 3; j++) {\n                sum += image[i][j];\n            }\n        }\n        return sum / 9;\n    }\n\n    void add(vector<vector<int>>& image, int row, int col, int val) {\n        for (int i = row; i < row + 3; i++) {\n            for (int j = col; j < col + 3; j++) {\n                image[i][j] += val;\n            }\n        }\n    }\n\n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int N = int(image.size());\n        int M = int(image[0].size());\n\n        vector<vector<int>> res(N, vector<int>(M));\n        vector<vector<int>> cnt(N, vector<int>(M));\n        for (int i = 0; i + 3 <= N; i++) {\n            for (int j = 0; j + 3 <= M; j++) {\n                if (!isRegion(image, i, j, threshold))\n                    continue;\n                \n                int avg = average(image, i, j);\n                add(res, i, j, avg);\n                add(cnt, i, j, 1);\n            }\n        }\n\n        for (int i = 0; i < N; i++) {\n            for (int j = 0; j < M; j++) {\n                if (cnt[i][j] > 0)\n                    res[i][j] /= cnt[i][j];\n                else\n                    res[i][j] = image[i][j];\n            }\n        }\n\n        return res;\n    }\n};\n```\n
['C++', 'Rust']