C++ | Prefix Sum
Code\n\nclass Solution {\n    int numOfFact(int n, int x) {\n        int count = 0;\n        while(n % x == 0) {\n            count++;\n            n /= x;\n   
# Code\n```\nclass Solution {\n    int numOfFact(int n, int x) {\n        int count = 0;\n        while(n % x == 0) {\n            count++;\n            n /= x;\n        }\n        return count;\n    }\npublic:\n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size(), ans = 0;\n        vector<vector<pair<int, int>>> h(m + 1, vector<pair<int, int>>(n + 1));\n        vector<vector<pair<int, int>>> v(m + 1, vector<pair<int, int>>(n + 1));\n        for(int i = 0; i < m; i++) {\n            for(int j = 0; j < n; j++) {\n                h[i + 1][j + 1].first = h[i + 1][j].first + numOfFact(grid[i][j], 2);  \n                h[i + 1][j + 1].second = h[i + 1][j].second + numOfFact(grid[i][j], 5); \n                v[i + 1][j + 1].first = v[i][j + 1].first + numOfFact(grid[i][j], 2);  \n                v[i + 1][j + 1].second = v[i][j + 1].second + numOfFact(grid[i][j], 5); \n            }\n        }\n        pair<int, int> h1, h2, v1, v2;\n        for(int i = 1; i <= m; i++) {\n            for(int j = 1; j <= n; j++) {\n                h1 = {h[i][j - 1].first, h[i][j - 1].second};\n                h2 = {h[i][n].first - h[i][j].first, h[i][n].second - h[i][j].second};\n                v1 = {v[i][j].first, v[i][j].second};\n                v2 = {v[m][j].first - v[i - 1][j].first, v[m][j].second - v[i - 1][j].second};\n                ans = max(ans, min(v1.first + h1.first, v1.second + h1.second));\n                ans = max(ans, min(v1.first + h2.first, v1.second + h2.second));\n                ans = max(ans, min(v2.first + h1.first, v2.second + h1.second));\n                ans = max(ans, min(v2.first + h2.first, v2.second + h2.second));\n            }\n        }\n        return ans;\n    }\n};\n```
['Matrix', 'Prefix Sum', 'C++']