Java Map Solution
\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        Map<String, Pair<Integer, Integer>> map = new HashMap<>();\n        f
```\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        Map<String, Pair<Integer, Integer>> map = new HashMap<>();\n        for (int i = 0; i < image.length - 2; i++) {\n            for (int j = 0; j < image[0].length - 2; j++) {\n                int sum = 0;\n                int f = 0;\n                for (int rowI = i; rowI < i + 3; rowI++) {\n                    for (int colJ = j; colJ < j + 3; colJ++) {\n                        int value = image[rowI][colJ];\n                        sum = sum + image[rowI][colJ];\n                        if (rowI + 1 < i + 3) {\n                            int diff = Math.abs(image[rowI][colJ] - image[rowI + 1][colJ]);\n                            if (diff > threshold) {\n                                f = 1;\n                                break;\n                            }   \n                        }\n                        if (colJ + 1 < j + 3) {\n                            int diff = Math.abs(image[rowI][colJ] - image[rowI][colJ + 1]);\n                            if (diff > threshold) {\n                                f = 1;\n                                break;\n                            }\n                        }\n                    }\n                    if (f == 1) {\n                        break;\n                    }\n                }\n                if (f == 1) {\n                    continue;\n                }\n                sum = sum / 9;\n                for (int rowI = i; rowI < i + 3; rowI++) {\n                    for (int colJ = j; colJ < j + 3; colJ++) {\n                        String pairKey = rowI + "-" + colJ;\n                        if (map.containsKey(pairKey)) {\n                            Pair<Integer, Integer> pairValue = map.get(pairKey);\n                            map.put(pairKey, new Pair<>(pairValue.getKey() + sum, pairValue.getValue() + 1));\n                        } else {\n                            map.put(pairKey, new Pair<>(sum, 1));\n                        }\n                    }\n                }\n            }\n        }\n        int[][] result = new int[image.length][image[0].length];\n        for (int i = 0; i < result.length; i++) {\n            for (int j = 0; j < result[0].length; j++) {\n                String pairKey = i + "-" + j;\n                if (!map.containsKey(pairKey)) {\n                    result[i][j] = image[i][j];\n                    continue;\n                }\n                Pair<Integer, Integer> pair = map.get(pairKey);\n                int sum = pair.getKey() / pair.getValue();\n                result[i][j] = sum;\n            }\n        }\n        return result;\n    }\n}\n```
['Java']