Solution Find the Grid of Region Average
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    /**\n     * @param Integer[][] $image\n     * @param Integer $threshold\n     * @return Integer[][]\n     */\n    function resultGrid($image, $threshold) {\n        $rows = count($image);\n        $cols = count($image[0]);\n        \n        $m = [];\n        for ($i = 0; $i < $rows - 2; $i++) {\n            for ($j = 0; $j < $cols - 2; $j++) {\n                $sum = 0;\n                $flag = false;\n                \n                for ($x = $i; $x < $i + 3; $x++) {\n                    for ($y = $j; $y < $j + 3; $y++) {\n                        $sum += $image[$x][$y];\n                        \n                        if ($x + 1 < $i + 3 && abs($image[$x][$y] - $image[$x + 1][$y]) > $threshold) {\n                            $flag = true;\n                            break 2;\n                        }\n                        \n                        if ($y + 1 < $j + 3 && abs($image[$x][$y] - $image[$x][$y + 1]) > $threshold) {\n                            $flag = true;\n                            break 2;\n                        }\n                    }\n                }\n                \n                if ($flag) continue;\n                \n                $sum = intval($sum / 9);\n                \n                for ($x = $i; $x < $i + 3; $x++) {\n                    for ($y = $j; $y < $j + 3; $y++) {\n                        if (isset($m[$x][$y])) {\n                            $m[$x][$y][0] += $sum;\n                            $m[$x][$y][1]++;\n                        } else {\n                            $m[$x][$y] = [$sum, 1];\n                        }\n                    }\n                }\n            }\n        }\n        \n        $result = [];\n        \n        for ($i = 0; $i < $rows; $i++) {\n            $result[$i] = [];\n            for ($j = 0; $j < $cols; $j++) {\n                if (isset($m[$i][$j])) {\n                    $result[$i][$j] = intval($m[$i][$j][0] / $m[$i][$j][1]);\n                } else {\n                    $result[$i][$j] = $image[$i][$j];\n                }\n            }\n        }\n        \n        return $result;\n    }\n}\n\n```
['PHP']