Java 🔥 Simple Solution✔
Code\n\nclass Solution {\n     public int[][] resultGrid(int[][] image, int threshold) {\n        int[][] regions = new int[image.length][image[0].length];\n   
# Code\n```\nclass Solution {\n     public int[][] resultGrid(int[][] image, int threshold) {\n        int[][] regions = new int[image.length][image[0].length];\n        int[][] avg = new int[image.length][image[0].length];\n\n        for (int i = 0; i <= image.length-3; i++) {\n            for (int j = 0; j <= image[i].length-3; j++) {\n                int checkRes = check(image,threshold,i,j);\n                if(checkRes != -1){\n                    int endRw = i +3;\n                    int endCol = j+3;\n                    for (int l = i; l < endRw && l < image.length; l++) {\n                        for (int m = j; m < endCol && m < image[l].length; m++) {\n                            regions[l][m]++;\n                            avg[l][m] += checkRes/9;\n                        }\n                    }\n                }\n            }\n        }\n\n        for (int i = 0; i < image.length; i++) {\n            for (int j = 0; j < image[i].length; j++) {\n                int region = regions[i][j] ;\n                if(region > 0){\n                    avg[i][j] = avg[i][j] / (region);\n                }else{\n                    avg[i][j] = image[i][j];                  \n                }\n            }\n        }\n\n        return avg;\n    }\n\n    public int check(int[][] image, int threshold, int row, int col){\n        int endRw = row +3;\n        int endCol = col+3;\n        int sum = 0;\n        for (int i = row; i < endRw && i < image.length; i++) {\n            for (int j = col; j < endCol && j < image[i].length; j++) {\n                if(j+1 < endCol && Math.abs(image[i][j] - image[i][j+1]) > threshold) return -1;\n                if(i+1 < endRw && Math.abs(image[i][j] - image[i+1][j]) > threshold) return -1;\n                sum += image[i][j];\n            }\n        }\n        return sum;\n    }\n}\n```
['Java']