✅ Simple Approach to Understand and Code | BF | Beats 95% in  Memory ✅
Intuition\n Describe your first thoughts on how to solve this problem. \nSince mines positions are given, the intuition is to build the grid and then calculate 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince mines positions are given, the intuition is to build the grid and then calculate max order at each position.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create a grid matrix and fill it with 1 and then update the value to 0 wherever mines are present.\n2. Declare mgrid (grid with mines) and len for global access in the class\n3. For each position in mgrid, calculate order using countOrder().\n4. countOrder function has order by default 1 value and then news variables (north, east, west, south) for direction to check 1 is there in the respective direction and in all the direction. So if the sum of all surrounding NEWS position for (i, j) then increment the counter and keep countin till in either of the directions, the loop goes beyond or sum is not 4.\n\nI hope, this helps:\n![image.png](https://assets.leetcode.com/users/images/66b86a8f-d538-4555-9833-5a4e4c4d84f8_1698380379.7166097.png)\n\n\n# Complexity\n- Time complexity: O(n^3) because n^2 for each position in the grid and n to go till the boundry of the grid.\n\n- Space complexity: O(n^2)\n\n# Code\n```\nclass Solution {\n    int len;\n    int[][] mgrid;\n    public int orderOfLargestPlusSign(int n, int[][] mines) {\n        len = n;\n        \n        mgrid = new int[n][n];\n        for(int i=0; i<n; i++)\n            Arrays.fill(mgrid[i], 1);\n\n        \n        for(int i=0; i<mines.length; i++){\n            int x = mines[i][0];\n            int y = mines[i][1];\n            mgrid[x][y] = 0;\n        }\n\n        int max = 0;\n        for(int i=0; i<n; i++){\n            for(int j=0; j<n; j++){\n                if(mgrid[i][j] == 1)\n                    max = Math.max(max, countOrder(i, j));\n            }\n        }\n        return max;\n    }\n\n    public int countOrder(int i, int j){\n        int order=1;\n       \n        int n=i-1, e=j+1, w=j-1, s=i+1;\n       \n        while(n>=0 && w>=0 && s<len && e<len){\n            if(mgrid[n][j] + mgrid[i][w] + mgrid[s][j] + mgrid[i][e] == 4)\n                order++;\n            else\n                break; \n            n--; w--; e++; s++;\n        }\n        \n        return order;\n    }\n}\n```
['Java']