'A command line option has enabled the Security Manager .... Leetcode

enter image description here

Leetcode Problem :

695: Max Area of Island

    class Solution {
        public int maxAreaOfIsland(int[][] grid) {
            int maxarea = 0;
            int n = grid.length; // row
            int m = grid[0].length; // column
    
            // Getting Area of island
            // check the grid with 1 and traverse through left, right, top and bottom
    
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (grid[i][j] == 1) {
                        int area = getArea(grid, i, j, n, m);
                        maxarea = Math.max(maxarea, area);
                    }
                }
            }
            return maxarea;
        }
    
        int getArea(int[][] grid, int i, int j, int n, int m) {
            // values are out of Island Area
            if (i < 0 || j < 0 || j > m || i > n || grid[i][j] == 0)
                return 0;
    
            grid[i][j] = 0; // marking the index 0 to avoid recounting grid
    
            // Recursive calling of Function to check values of left ,right ,top and bottom
            // of grid index
    
            int left = getArea(grid, i - 1, j, m, n);
            int right = getArea(grid, i + 1, j, m, n);
            int top = getArea(grid, i, j + 1, m, n);
            int bottom = getArea(grid, i, j - 1, m, n);
    
            int area = left + right + top + bottom + 1;// 1 for island which is marked 0
    
            return area;
        }
    
    } 

Error:

WARNING: A command line option has enabled the Security Manager
WARNING: The Security Manager is deprecated and will be removed in a future release
java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
  at line 28, Solution.getArea
  at line 39, Solution.getArea
  at line 39, Solution.getArea
  at line 16, Solution.maxAreaOfIsland
  at line 54, __DriverSolution__.__helper__
  at line 84, __Driver__.main 


Solution 1:[1]

Your range check in getArea() is broken. It checks that the indices are in the range 0 to grid.length or 0 to grid[0].length, but the valid indices are only 0 to grid.length-1 or 0 to grid[0].length-1

Change the index check to

   if (i < 0 || j < 0 || j >= m || i >= n || grid[i][j] == 0)
       return 0;

The two WARNING lines are caused by the way the leetcode starts the JVM - there is nothing that you can do about it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas Kläger