Java Simple & Easy | lengthy but straight forward
Approach:\n- IMPORTANT: Idea is to keep track of count[]\n- Create a result[][] of size m and n\n- For each grid 3x3 possible, calculate the average\n\t- If VAL
# Approach:\n- **IMPORTANT**: Idea is to keep track of `count[]`\n- Create a `result[][]` of size `m and n`\n- For each grid `3x3` possible, calculate the average\n\t- If **VALID** (when `currentAverage != MIN_VALUE`)\n\t\t- Update the result adding the `currentAverage` and increment `count[i][j]++`\n- **POST processing**\n\t- if the result != MIN_VALUE update the result with `sum/count` for `(i,j)`\n\t- else put the original value from `image[i][j]`\n\n```\n\nclass Solution {\n    int m = 0, n = 0;\n    int[][] result = null;\n    int[][] count = null;\n    public int[][] resultGrid(int[][] image, int threshold) {\n        m = image.length; n = image[0].length;\n        result = new int[m][n];\n        count = new int[m][n];\n        \n\t\t// For each possible grid of 3x3\n        for(int col = 0; col <= n-3; col++) {\n            for(int row = 0; row <= m-3; row++) {\n                int currentAverage = calculateCurrentAverage(image, row, col, threshold);\n                if(currentAverage != Integer.MIN_VALUE) // it means current grid is valid\n                    updateAverage(image, row, col, currentAverage, threshold);\n            }\n        }\n        \n        for(int i = 0; i < m; i++)\n            for(int j = 0; j < n; j++)\n                if(count[i][j] != 0) // When VALID\n                    result[i][j] = (result[i][j] / count[i][j]);\n                else\n                    result[i][j] = image[i][j]; // When INVALID: update with original value\n        \n        return result;\n    }\n    \n    public void updateAverage(int[][] image, int row, int col, int currentAverage, int threshold) {\n        for(int i = row; i < row+3; i++) {\n            for(int j = col; j < col+3; j++) {\n                  result[i][j] += currentAverage;\n                count[i][j]++;\n            }\n        }\n    }\n    \n    public int calculateCurrentAverage(int[][] image, int row, int col, int threshold) {\n        int tempSum = 0;\n        for(int i = row; i < row+3; i++) {\n            for(int j = col; j < col+3; j++) {\n\t\t\t\t// If the current position is invalid, the current grid is invalid too, so return MIN_VALUE as average\n                if(!isCurrentPositionValid(image, i, j, threshold, row, col))\n                    return Integer.MIN_VALUE;\n                tempSum += image[i][j];\n            }\n        }\n        return tempSum/9;\n    }\n    \n    public boolean isCurrentPositionValid(int[][] image, int row, int col, int threshold, int startRow, int startCol) {\n        if(row - 1 >= startRow && Math.abs(image[row-1][col] - image[row][col]) > threshold)\n            return false;\n        if(row + 1 <= startRow + 2 && Math.abs(image[row+1][col] - image[row][col]) > threshold)\n            return false;\n        if(col - 1 >= startCol && Math.abs(image[row][col-1] - image[row][col]) > threshold)\n            return false;\n        if(col + 1 <= startCol + 2 && Math.abs(image[row][col+1] - image[row][col]) > threshold)\n            return false;\n        return true;\n    }\n}\n```
['Array', 'Java']