Java | DFS solution with comments | 1 ms
\nclass Solution {\n    public int[] findBall(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int[] ans = new int[n];\n 
```\nclass Solution {\n    public int[] findBall(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int[] ans = new int[n];\n        \n        for(int j=0;j<n;j++)\n            ans[j] = checkBallFallsOut(grid, 0, j, m, n);\n        return ans;\n    }\n    \n    private int checkBallFallsOut(int[][] grid, int i, int j, int m, int n){\n        if(i<0 || j<0 || i>m || j>=n)\n            return -1;\n        \n\t\t//If we have reached to end of the rows then return column.\n        if(i==m)\n            return j;\n        \n        if(grid[i][j]==-1){\n\t\t\t//Check for V shape.\n            if(j>0 && grid[i][j-1] == 1)\n                return -1;\n            return checkBallFallsOut(grid, i+1, j-1, m, n);\n        }\n        else{\n\t\t\t//Check for V Shape.\n            if(j<n-1 && grid[i][j+1] == -1)\n                return -1;\n            return checkBallFallsOut(grid, i+1, j+1, m, n);\n        }\n    }\n}\n\n***** PLEASE UPVOTE IF YOU LIKE THE SOLUTION*****\n```
['Depth-First Search', 'Java']