Java 99% Faster|| Math || Logic || Counting
Please upvote if you like the Solution :)\n\n# Code\n\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        int n=image.leng
# ***Please upvote if you like the Solution :)***\n\n# Code\n```\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        int n=image.length;\n        int m=image[0].length;\n        boolean status[][]=new boolean[n][m];\n        int intensity[][]=new int[n][m];\n        int count[][]=new int[n][m];\n        \n    \n        \n        for(int i=1;i<n-1;i++){\n            for(int j=1;j<m-1;j++){\n                \n                int curri=i;\n                int currj=j;\n                //System.out.println(curri+" "+currj);\n                \n                boolean regionPossible=true;\n                int regionSum=0;\n                \n                int r0c0=image[curri-1][currj-1];\n                int r0c1=image[curri-1][currj];\n                int r0c2=image[curri-1][currj+1];\n                int r1c0=image[curri][currj-1];\n                int r1c1=image[curri][currj];\n                int r1c2=image[curri][currj+1];\n                int r2c0=image[curri+1][currj-1];\n                int r2c1=image[curri+1][currj];\n                int r2c2=image[curri+1][currj+1];\n                \n                regionSum+=(r0c0+r0c1+r0c2+r1c0+r1c1+r1c2+r2c0+r2c1+r2c2);\n                \n                if(Math.abs(r0c0-r0c1)>threshold ||\n                   Math.abs(r0c0-r1c0)>threshold ||\n                   Math.abs(r0c1-r0c0)>threshold ||\n                   Math.abs(r0c1-r1c1)>threshold ||\n                   Math.abs(r0c1-r0c2)>threshold ||\n                   Math.abs(r0c2-r0c1)>threshold ||\n                   Math.abs(r0c2-r1c2)>threshold ||\n                   \n                   Math.abs(r1c0-r1c1)>threshold ||\n                   \n                   Math.abs(r1c2-r1c1)>threshold ||\n                  \n                   Math.abs(r2c0-r2c1)>threshold ||\n                   Math.abs(r2c0-r1c0)>threshold ||\n                   Math.abs(r2c1-r2c0)>threshold ||\n                   Math.abs(r2c1-r1c1)>threshold ||\n                   Math.abs(r2c1-r2c2)>threshold ||\n                   Math.abs(r2c2-r2c1)>threshold ||\n                   Math.abs(r2c2-r1c2)>threshold \n                   )\n                    regionPossible=false;\n                \n                if(regionPossible){\n                    //System.out.println(curri+" "+currj);\n                    regionSum/=9;\n                    for(int k=-1;k<=1;k++){\n                        for(int l=-1;l<=1;l++){\n                            intensity[curri+k][currj+l]+=regionSum;\n                            count[curri+k][currj+l]++;\n                        }\n                    }\n                }\n                \n            }\n        }\n        \n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                if(count[i][j]==0){\n                    intensity[i][j]=image[i][j];\n                }\n                else{\n                    intensity[i][j]=intensity[i][j]/count[i][j];\n                }\n            }\n        }\n        return intensity;\n        \n    }\n}\n```
['Java']