C++ || 100% fastest!  || Easy understanding with picture
This question is asking about \n "Trailing zeros"\n "Cornered Path"\n\nBasic idea 1: we only care about the count of 2 and the count of 5.\nFor instance, if we 
This question is asking about \n* "Trailing zeros"\n* "Cornered Path"\n\n**Basic idea 1: we only care about the count of 2 and the count of 5.**\nFor instance, if we have "{2,8,10,30}" => 2 x 8 x10x 30 = 4800 => trailing zeros = 2\nWe can transfer this array into the count of 2 and 5\ncount of 2: {1,3,1,2} => prefix sum of 2: {1,4,5,7}\ncount of 5: {0,0,1,1} => prefix sum of 5: {0,0,1,2}\nTherefore, the trailing zeros will be  minimum(7,2) = 2\n\nAfter basic idea 1, we can create two matrix\nQuestion\n![image](https://assets.leetcode.com/users/images/23cf94ee-e461-4556-9e3d-d6183b8e781c_1650289746.9719374.png)\n\nTrafnser into\n![image](https://assets.leetcode.com/users/images/1615924b-86bd-4fc3-92fd-686101bfde57_1650289803.599491.png)\n![image](https://assets.leetcode.com/users/images/373ff873-ab78-4ff2-bf51-c238700f43e8_1650289818.1484864.png)\n\n\n**Basic idea 2: Cornered Path**\n![image](https://assets.leetcode.com/users/images/f39b0c56-2fc4-45c2-ba12-48c575d5b7ce_1650290157.0635967.png)\nThe answer will be the minimun of these for cases.\n\nWe can use "Horizontal prefix sum" matrix and "Vertical prefix sum" matrix ot get the result of these four cases.\n![image](https://assets.leetcode.com/users/images/964cf894-c5a7-4b77-a141-52800833a1a4_1650290857.2102344.png)\n\nFor example, we can use prefix sum to gernerate a result\nthe result will be min( horizontal2, horizantal 5) + min (vertical 2, vertical 5)\n![image](https://assets.leetcode.com/users/images/f70197a9-02b1-4a20-9b7e-616255e439c8_1650291569.4562247.png)\n\nIn this case, the answer will be\nmin (3,1) + min(3,2) = 1 + 2 = 3\n\nThat\'s the whole idea!\n\n\n**My implementation:**\n\n```\nint maxTrailingZeros(vector<vector<int>>& grid) {\n        int result = 0;\n\n\t\t// gernerate matrix of 2 and matrix of 5\n        vector< vector<int> > dp2(grid.size(), vector<int>(grid[0].size(), 0));\n        vector< vector<int> > dp5(grid.size(), vector<int>(grid[0].size(), 0));\n        for (int i = 0; i < grid.size(); i++) {\n            for (int j = 0; j < grid[i].size(); j++) {\n                int tmp = grid[i][j];\n                while (tmp % 2 == 0) {\n                    tmp = tmp / 2;\n                    dp2[i][j]++;\n                }\n                while (tmp % 5 == 0) {\n                    tmp = tmp / 5;\n                    dp5[i][j]++;\n                }\n            }\n        }        \n        \n        vector< vector<int> > dp2HorizontalSum(grid.size(), vector<int>(grid[0].size(), 0));\n        vector< vector<int> > dp5HorizontalSum(grid.size(), vector<int>(grid[0].size(), 0));\n        \n\t\t// Generate horizontal prefix sum array\n        for (int i = 0; i < dp2.size(); i++) {\n            for (int j = 0; j < dp2[0].size(); j++) {\n                if (j == 0) {\n                    dp2HorizontalSum[i][j] = dp2[i][j];\n                    dp5HorizontalSum[i][j] = dp5[i][j];\n                } else {\n                    dp2HorizontalSum[i][j] = dp2HorizontalSum[i][j - 1] + dp2[i][j];\n                    dp5HorizontalSum[i][j] = dp5HorizontalSum[i][j - 1] + dp5[i][j];\n                }\n\n            }\n        }    \n        \n\t\t// Generate vertical prefix sum array\n        vector< vector<int> > dp2VerticalSum(grid.size(), vector<int>(grid[0].size(), 0));\n        vector< vector<int> > dp5VerticalSum(grid.size(), vector<int>(grid[0].size(), 0));\n        for (int i = 0; i < dp2.size(); i++) {\n            for (int j = 0; j < dp2[0].size(); j++) {\n                if (i == 0) {\n                    dp2VerticalSum[i][j] = dp2[i][j];\n                    dp5VerticalSum[i][j] = dp5[i][j];\n                } else {\n                    dp2VerticalSum[i][j] = dp2VerticalSum[i - 1][j] + dp2[i][j];\n                    dp5VerticalSum[i][j] = dp5VerticalSum[i - 1][j] + dp5[i][j];\n                }\n            }\n        }\n        \n\t\t// Iterate every element and pick the biggest one to be the result.\n        for (int i = 0; i < dp2.size(); i++) {\n            for (int j = 0; j < dp2[0].size(); j++) {\n                int dp2H1 = dp2HorizontalSum[i][j];\n                int dp2H2 = dp2HorizontalSum[i][dp2[0].size() -1] - dp2HorizontalSum[i][j] + dp2[i][j];\n                int dp2V1 = dp2VerticalSum[i][j];\n                int dp2V2 = dp2VerticalSum[dp2.size() - 1][j] - dp2VerticalSum[i][j] + dp2[i][j];\n                \n                int dp5H1 = dp5HorizontalSum[i][j];\n                int dp5H2 = dp5HorizontalSum[i][dp5[0].size()  -1] - dp5HorizontalSum[i][j] + dp5[i][j];\n                int dp5V1 = dp5VerticalSum[i][j];\n                int dp5V2 = dp5VerticalSum[dp5.size() - 1][j] - dp5VerticalSum[i][j] + dp5[i][j];\n                \n                int tmp = max(  min(dp2H1 + dp2V1 - dp2[i][j], dp5H1 + dp5V1 - dp5[i][j]), min(dp2H1 + dp2V2 - dp2[i][j], dp5H1 + dp5V2 - dp5[i][j]) );\n                tmp = max(tmp, min(dp2H2 + dp2V1 - dp2[i][j], dp5H2 + dp5V1 - dp5[i][j]));\n                tmp = max(tmp, min(dp2H2 + dp2V2 - dp2[i][j], dp5H2 + dp5V2 - dp5[i][j]));\n                \n                if (tmp > result) {\n                    result = tmp;\n                }\n            }\n        }\n        return result;\n    }\n```
['Prefix Sum']