[C++] Prefix Sum of factors 2 and 5 (Used 3D array to make implementation easier)
\n\n#define endl ("\\n")\n#define pi (3.141592653589)\n#define mod 1e9+7\n#define ll long long\n#define float double\n#define pb push_back\n#define pp pair<ll, 
```\n\n#define endl ("\\n")\n#define pi (3.141592653589)\n#define mod 1e9+7\n#define ll long long\n#define float double\n#define pb push_back\n#define pp pair<ll, ll>\n#define mp make_pair\n#define ff first\n#define ss second\n#define all(c) cc.begin(), c.end()\n#define mini(a, b, c) min(c, min(a, b))\n#define rrep(i, n) for(int i=n-1;i>=0;i--)\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep1(i,n) for(int i=1;i<n;i++)\n\nclass Solution {\npublic:\n    pp solve(ll ele){\n        int temp = ele;\n        int twos = 0, fives = 0;\n        while(temp%2==0){\n            twos++;\n            temp/=2;\n        }\n        temp = ele;\n        while(temp%5==0){\n            fives++;\n            temp/=5;\n        }\n        return {twos, fives};\n    }\n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int n = grid.size();\n        int m = grid[0].size();\n        vector<vector<vector<pp>>> dp(n, vector<vector<pp>>(m, vector<pp>(4)));\n        rep(i, n){\n            vector<pp> store(m);  //{2, 5}\n            rep(j, m){\n                pp it = solve(grid[i][j]);\n                if(j == 0){\n                    store[j] = it;\n                }else{\n                    store[j] = {it.first+store[j-1].first, it.second+store[j-1].second};\n                }\n            }\n            int t_2 = store[m-1].first, t_5 = store[m-1].second;\n            rep(j, m){\n                dp[i][j][0].first = store[j].first;\n                dp[i][j][0].second = store[j].second;\n                dp[i][j][1].first = t_2 - (j >= 1 ? store[j-1].first:0);\n                dp[i][j][1].second = t_5 - (j >= 1 ? store[j-1].second:0);\n            }\n        }\n\n        rep(j, m){\n            vector<pp> store(n);  //{2, 5}\n            rep(i, n){\n                pp it = solve(grid[i][j]);\n                if(i == 0){\n                    store[i] = it;\n                }else{\n                    store[i] = {it.first+store[i-1].first, it.second+store[i-1].second};\n                }\n            }\n            int t_2 = store[n-1].first, t_5 = store[n-1].second;\n            rep(i, n){\n                dp[i][j][2].first = store[i].first;\n                dp[i][j][2].second = store[i].second;\n                dp[i][j][3].first = t_2 - (i >= 1 ? store[i-1].first:0);\n                dp[i][j][3].second = t_5 - (i >= 1 ? store[i-1].second:0);\n            }\n        }\n        \n        int res = 0;\n        rep(i, n){\n            rep(j, m){\n                int up_max = min(dp[i][j][2].first, dp[i][j][2].second);\n                int down_max = min(dp[i][j][3].first, dp[i][j][3].second);\n                \n                int left_max = min(dp[i][j][0].first, dp[i][j][0].second);\n                int right_max = min(dp[i][j][1].first, dp[i][j][1].second);\n                \n                pp it = solve(grid[i][j]);\n                //UP -> RIGHT\n                ll twos = dp[i][j][2].first+dp[i][j][1].first-it.first;\n                ll fives = dp[i][j][2].second+dp[i][j][1].second-it.second;\n                \n                ll tu = min(twos, fives);\n                res = res < tu ? tu:res;\n                \n                //UP -> LEFT\n                twos = dp[i][j][2].first+dp[i][j][0].first-it.first;\n                fives = dp[i][j][2].second+dp[i][j][0].second-it.second;\n                \n                tu = min(twos, fives);\n                res = res < tu ? tu:res;\n                \n                //LEFT -> DOWN\n                twos = dp[i][j][0].first+dp[i][j][3].first-it.first;\n                fives = dp[i][j][0].second+dp[i][j][3].second-it.second;\n                \n                tu = min(twos, fives);\n                res = res < tu ? tu:res;\n                \n                //DOWN -> RIGHT\n                twos = dp[i][j][3].first+dp[i][j][1].first-it.first;\n                fives = dp[i][j][3].second+dp[i][j][1].second-it.second;\n                \n                tu = min(twos, fives);\n                res = res < tu ? tu:res;\n            }\n        }\n        \n        return (int)res;\n    }\n};\n\n```
['C', 'Matrix', 'Prefix Sum']