Easy & Clean Solution
```java\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        int m = image.length, n = image[0].length;\n        int[][] re
```java\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        int m = image.length, n = image[0].length;\n        int[][] result = new int[m][n];\n        int[][] count = new int[m][n];\n        for(int i = 0; i<m-2; i++){\n            for(int j = 0; j< n-2; j++){\n                int avg = isGrid(i, j, image, threshold);\n                if(avg > -1){\n                    fillResult(i, j, result, count,  avg);\n                }\n            }\n        }\n        makeAvg(result, count, image,m,n);\n        return result;\n    }\n    \n    private void makeAvg(int[][] result, int count[][], int[][] image, int m, int n){\n        for(int i = 0; i<m; i++){\n            for(int j = 0; j<n; j++){\n                if(count[i][j] == 0){\n                    result[i][j] = image[i][j];\n                }else{\n                    result[i][j] = result[i][j]/ count[i][j];\n                }\n            }\n        }\n    }\n    \n    private void fillResult(int x, int y, int[][] result, int[][] count, int avg){\n        for(int i = x; i < x+3; i++){\n            for(int j = y; j < y+3; j++ ){\n                result[i][j] += avg;\n                count[i][j]++;\n            }\n        }\n    }\n    \n    private int isGrid(int x, int y, int image[][], int threshold){\n        int avg = 0;\n        for(int i =x; i<x+3; i++){\n            for(int j = y; j < y+3; j++){\n                if(i < x+2 ){\n                   if(Math.abs(image[i][j] - image[i+1][j]) > threshold){\n                       return -1;\n                    } \n                }\n                if(j < y+2){\n                     if(Math.abs(image[i][j] - image[i][j+1]) > threshold){\n                       return -1;\n                    } \n                }\n                avg += image[i][j];\n            }\n        }\n        return avg/9;\n    }\n}\n\n\n````
['Java']