c++ || 100% memory || 100% faster  prefix sum of 2s,5s in prime factorization of grid element
\nclass Solution {\npublic:\n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size();\n        pair<int,int> zero
```\nclass Solution {\npublic:\n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size();\n        pair<int,int> zero = make_pair(0,0);\n        vector<vector<pair<int,int>>> prefix(m,vector<pair<int,int>>(n,zero));\n        // (2s,5s)\n        for(int i=0; i<m; i++){\n            \n            for(int j=0; j<n; j++){\n                int temp = grid[i][j];\n                while(temp%2 ==0){\n                    prefix[i][j].first = prefix[i][j].first + 1;\n                    temp = temp/2;\n                }\n                temp = grid[i][j];\n                while(temp%5 ==0){\n                    prefix[i][j].second = prefix[i][j].second + 1;\n                    temp = temp/5;\n                }\n            }\n        }\n        \n        for(int i=0; i<m; i++){\n            for(int j=1; j<n; j++){\n                prefix[i][j].first = prefix[i][j].first + prefix[i][j-1].first;\n                prefix[i][j].second = prefix[i][j].second + prefix[i][j-1].second;\n            }\n        }\n        \n        int maxzeros = 0;\n        // if ans is a smaller L, a bigger L containing the smaller L can either increase the \n        // twos and fives or remain same.  4 possible L shapes\n        \n        // top to bottom , consider left or right\n        for(int j=0; j<n; j++){\n            pair<int,int> ver= zero;\n            int twos=0, fives=0;\n            \n            for(int i=0; i<m; i++){\n                twos = ver.first;\n                fives = ver.second;\n                // current element\n                twos += prefix[i][j].first - (j>0?prefix[i][j-1].first:0);\n                fives += prefix[i][j].second - (j>0?prefix[i][j-1].second:0);\n                ver.first = twos;\n                ver.second = fives;\n                // left\n                twos += j>0?prefix[i][j-1].first:0;\n                fives += j>0?prefix[i][j-1].second:0;\n                maxzeros = max(maxzeros,min(twos,fives));\n                // right\n                twos = ver.first;\n                fives = ver.second;\n                twos += prefix[i][n-1].first-prefix[i][j].first;\n                fives += prefix[i][n-1].second-prefix[i][j].second;\n                \n                maxzeros = max(maxzeros,min(twos,fives));\n            }\n        }\n        \n        // bottom to top. consider either left or right\n        for(int j=0; j<n; j++){\n            pair<int,int> ver= zero;\n            int twos=0, fives=0;\n            \n            for(int i=m-1; i>=0; i--){\n                twos = ver.first;\n                fives = ver.second;\n                // current element\n                twos += prefix[i][j].first - (j>0?prefix[i][j-1].first:0);\n                fives += prefix[i][j].second - (j>0?prefix[i][j-1].second:0);\n                ver.first = twos;\n                ver.second = fives;\n                // left\n                twos += j>0?prefix[i][j-1].first:0;\n                fives += j>0?prefix[i][j-1].second:0;\n                maxzeros = max(maxzeros,min(twos,fives));\n                // right\n                twos = ver.first;\n                fives = ver.second;\n                twos += prefix[i][n-1].first-prefix[i][j].first;\n                fives += prefix[i][n-1].second-prefix[i][j].second;\n                \n                maxzeros = max(maxzeros,min(twos,fives));\n            }\n        }\n        \n        return maxzeros;\n        \n    }\n};\n```
[]