C++ || Easy || Explanation
\nclass Solution {\npublic:\n    // Add Pair Utility Function\n    pair<int, int> addPair(pair<int, int> a, pair<int, int> b){\n        return {a.first + b.firs
```\nclass Solution {\npublic:\n    // Add Pair Utility Function\n    pair<int, int> addPair(pair<int, int> a, pair<int, int> b){\n        return {a.first + b.first , a.second + b.second};\n    }\n    // Sub Pair Utility Function\n    pair<int, int> subPair(pair<int, int> a, pair<int, int> b){\n        return {a.first - b.first , a.second - b.second};\n    }\n    \n    // Find the number of 5s and 2s making factors of the current number\n    pair<int, int> factors(int num){\n        int fiveCount = 0;\n        int twoCount = 0;\n        while(num % 5 == 0){\n            fiveCount += 1;\n            num /= 5;\n        }\n        while(num % 2 == 0){\n            twoCount += 1;\n            num /= 2;\n        }\n        return {fiveCount, twoCount};\n    }\n    \n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int n = grid.size();\n        int m = grid[0].size();\n        int ans = 0;\n        vector<vector<pair<int, int>>> hor(n, vector<pair<int, int>>(m, {0, 0}));\n        vector<vector<pair<int, int>>> ver(n, vector<pair<int, int>>(m, {0, 0}));\n        vector<vector<pair<int, int>>> fgrid(n, vector<pair<int, int>>(m, {0, 0}));\n        // Get count of 5 and 2 factors of each number\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                fgrid[i][j] = factors(grid[i][j]);\n            }\n        }\n        \n        // horizontal prefix array from left to right\n        for(int i=0;i<n;i++){\n            int fcount = 0;\n            int tcount = 0;\n            for(int j=0;j<m;j++){\n                fcount += fgrid[i][j].first;\n                tcount += fgrid[i][j].second;\n                hor[i][j] = {fcount, tcount};\n            }\n        }\n        \n        // vertical prefix array from top to bottom \n        for(int i=0;i<m;i++){\n            int fcount = 0;\n            int tcount = 0;\n            for(int j=0;j<n;j++){\n                fcount += fgrid[j][i].first;\n                tcount += fgrid[j][i].second;\n                ver[j][i] = {fcount, tcount};\n            }\n        }\n        \n        /*\n        *   Iterating each cell. There are 4 ways each cell can contribute to the path\n        *   top-left, top-right, left-bottom, right-bottom\n        *   Adding 5s and 2s for all paths and updating ans with min count of 5s ans 2s.\n        */\n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++){\n                pair<int, int> count = {0, 0};\n                count = subPair(addPair(hor[i][j], ver[i][j]), fgrid[i][j]);\n                ans = max(ans, min(count.first, count.second));\n                \n                count = {0, 0};\n                count = subPair(addPair(hor[i][m-1], ver[i][j]), hor[i][j]);\n                ans = max(ans, min(count.first, count.second));\n\n                count = {0, 0};\n                count = subPair(addPair(hor[i][j], ver[n-1][j]), ver[i][j]);\n                ans = max(ans, min(count.first, count.second));\n                \n                count = {0, 0};\n                count = addPair(subPair(subPair(addPair(hor[i][m-1], ver[n-1][j]), hor[i][j]), ver[i][j]), fgrid[i][j]);\n                ans = max(ans, min(count.first, count.second));\n                \n            }\n        }\n        \n\n        return ans;\n    }\n};\n```
['Matrix']