c++ factor 2 and 5  time: O(nm)  space: O(m)
\nclass Solution {\n    void update(int gridv, vector<int>& c)\n    {\n        while (gridv != 0 && gridv % 5 == 0) {c[0]++; gridv/=5;}\n        while (gridv !=
```\nclass Solution {\n    void update(int gridv, vector<int>& c)\n    {\n        while (gridv != 0 && gridv % 5 == 0) {c[0]++; gridv/=5;}\n        while (gridv != 0 && gridv % 2 == 0) {c[1]++; gridv/=2;}\n    }\n    \n    int calculate(vector<vector<int>>& grid, bool updown)\n    {\n        int n = grid.size(), m = grid[0].size();\n        int i=updown?0:n-1;\n        int ans = 0;\n        vector<vector<int>> count(m,vector<int>(2,0));//count of 2, count of  5\n        while(i>=0 && i <n)\n        {\n            vector<int> hcount = {0,0};\n            for(int j=0; j<m; j++)\n            {\n                update(grid[i][j], count[j]);\n                ans = max(ans, min(count[j][0] + hcount[0], count[j][1] + hcount[1]));\n                update(grid[i][j], hcount);\n            }\n            hcount[0] = hcount[1] = 0;\n            for(int j=m-1; j>=0; j--)\n            {\n                ans = max(ans, min(count[j][0] + hcount[0], count[j][1] + hcount[1]));\n                update(grid[i][j], hcount);\n            }\n            i+= updown?1:-1;\n        }\n        return ans;\n    }\n    \npublic:\n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        return max(calculate(grid, true), calculate(grid, false));\n    }\n};\n```
[]