✅ [C++/Python/Java] Simple Solution || One-Pass || Image Explanation
PLEASE UPVOTE if you like \uD83D\uDE01 If you have any question, feel free to ask. \n First count the number of 1 in each layer as Ni\n skip or remove the layer
**PLEASE UPVOTE if you like** \uD83D\uDE01 **If you have any question, feel free to ask.** \n* First count the number of `1` in each layer as N<sub>i<sub>\n* skip or remove the layers which do not contain `1`\n* So the number of connections between any two adjacent layers is N<sub>i</sub> * N<sub>i-1</sub>\n\t* return the sum of  N<sub>i</sub> * N<sub>i-1</sub>\n\n![image](https://assets.leetcode.com/users/images/5f84dff4-9389-470a-bedf-cecbe8dba904_1641110803.5347712.png)\n\n```\nTime  Complexity: O(MN)\nSpace Complexity: O(1)\n```\n\n**C++**\n```\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        int ans = 0, pre = 0;\n        for (int i = 0;i < bank.size(); i ++) {\n            int n = count(bank[i].begin(), bank[i].end(), \'1\');\n            if (n == 0) continue;\n            ans += pre * n;;\n            pre = n;\n        }\n        return ans;\n    }\n};\n```\n**Python**\n```\nclass Solution(object):\n    def numberOfBeams(self, bank):\n        ans, pre = 0, 0\n        for s in bank:\n            n = s.count(\'1\')\n            if n == 0: continue\n            ans += pre * n\n            pre = n\n        return ans\n```\n**Java**\n```\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        int ans = 0, pre = 0;\n        for (int i = 0;i < bank.length; i ++) {\n            int n = 0;\n            for (int j = 0; j < bank[i].length(); j ++) if(bank[i].charAt(j) == \'1\') n ++;\n            if (n == 0) continue;\n            ans += pre * n;;\n            pre = n;\n        }\n        return ans;\n    }\n}\n```\n\n**PLEASE UPVOTE if you like** \uD83D\uDE01 **If you have any question, feel free to ask.**
['C', 'Python', 'Java']