Java Simple and Easily Understood Solution
The idea is to first find the first row/string that has atleast one security device.Count the no of devices and store it into variable firstRow. After finding t
**The idea is to first find the first row/string that has atleast one security device.Count the no of devices and store it into variable firstRow. After finding that row, find the next closest row/string that has atleast one security device and store it into the variable secondRow. Then multiply firstRow with secondRow and add it to the answer.**\n\nImplement the above idea until we reach the last row/element of string array and then return the final answer.\n\n```\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        if(bank.length < 2)\n            return 0;\n        int ans = 0;\n        int firstRow = 0;\n        int j = 0;\n        \n        // First find first row that has atleast 1 security device\n        for(int i=0; i<bank.length; i++) {\n            firstRow = countOnes(bank[i]);\n            if(firstRow != -1) {\n                j = i;\n                break;\n            }\n            if(i == bank.length)\n                return 0;\n        }\n        \n        // Then find the closest next row that has atleast 1 security device and then add their product to the \'ans\'. After that, change firstRow to secondRow and check for remaining rows.\n        for(int k=j+1; k<bank.length; k++) {\n            int secondRow = countOnes(bank[k]);\n            if(secondRow != -1) {\n                ans += (firstRow * secondRow);\n                firstRow = secondRow;\n            }\n        }\n    return ans;\n    }\n    \n    // Count the no of security devices present in a row.\n    private int countOnes(String s) {\n        int a = 0;\n        for(int i=0; i<s.length(); i++) {\n            if(s.charAt(i) == \'1\')\n                a++;\n        }\n        return a > 0 ? a : -1;\n    }\n}\n```\n\n**Please upvote if this helps you :)**
['Java']