C++ O(n*m) prefix sum solution (100%)
C++\nclass Solution {\n    int countWith(int& n, int divisor) {\n        int res = 0;\n        while(!(n % divisor)) {\n            n /= divisor;\n            r
```C++\nclass Solution {\n    int countWith(int& n, int divisor) {\n        int res = 0;\n        while(!(n % divisor)) {\n            n /= divisor;\n            res++;\n        }\n        return res;\n    }\npublic:\n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int n = grid.size(), m = grid[0].size(), res = 0;\n        vector<vector<pair<int,int>>> rowPrefixSum(n, vector<pair<int,int>>(m, {0, 0})), colPrefixSum(n, vector<pair<int,int>>(m, {0, 0})), count(n, vector<pair<int,int>>(m, {0, 0}));\n\n        for(int i = 0; i < n; i++) {\n            for(int j = 0; j < m; j++) {\n                int n = grid[i][j], ct = countWith(n, 2), cf = countWith(n, 5);\n                \n                count[i][j] = {ct, cf};\n                if(j) rowPrefixSum[i][j] = rowPrefixSum[i][j - 1];\n                if(i) colPrefixSum[i][j] = colPrefixSum[i - 1][j];\n                rowPrefixSum[i][j].first += ct, rowPrefixSum[i][j].second += cf;\n                colPrefixSum[i][j].first += ct, colPrefixSum[i][j].second += cf;\n            }\n        }\n\n        for(int i = 0; i < n; i++) {\n            for(int j = 0; j < m; j++) {\n                auto [upt, upf] = colPrefixSum[i][j];\n                auto lot = colPrefixSum[n - 1][j].first - (i ? colPrefixSum[i - 1][j].first : 0), lof = colPrefixSum[n - 1][j].second - (i ? colPrefixSum[i - 1][j].second : 0);\n                auto [let, lef] = rowPrefixSum[i][j];\n                auto rit = rowPrefixSum[i][m - 1].first - (j ? rowPrefixSum[i][j - 1].first : 0), rif = rowPrefixSum[i][m - 1].second - (j ? rowPrefixSum[i][j - 1].second : 0);\n\n                res = max({res,\n                           min(upt + let - count[i][j].first, upf + lef - count[i][j].second), //from up to left \n                           min(upt + rit - count[i][j].first, upf + rif - count[i][j].second), //from up to right\n                           min(lot + let - count[i][j].first, lof + lef - count[i][j].second), //from bottom to left\n                           min(lot + rit - count[i][j].first, lof + rif - count[i][j].second) //from bottom to right\n                          });\n            }\n        }\n        return res;\n    }\n};\n```
[]