[Python] Explanation with pictures, prefix sum.
Please also refer to votrubac \'s solution as he has a perfect picture explanation of prefix sum.\n\nLet\'s focus on the pivot point of an edge, once the pivot 
Please also refer to [votrubac](https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/discuss/1955515/Prefix-Sum-of-Factors-2-and-5) \'s solution as he has a perfect picture explanation of prefix sum.\n\nLet\'s focus on the pivot point of an edge, once the pivot point is fixed, the edge can only from the four cases below.\n\n![image](https://assets.leetcode.com/users/images/b91a94f3-ca21-49d7-af85-662f80023fa0_1650169235.6731899.png)\n\nIt look familiar, we can build two (or four if you prefer) prefix sum array to save the cumulative factors. \nImagine we build `left` to save the all the factors in the same row on `A[i][j]`\'s left, and build `top` to save all the factors in the same colomn on `A[i][j]`\'s top. Notice we might add or minus the `A[i][j]` itself twice.\n\n\n![image](https://assets.leetcode.com/users/images/20d53765-2b44-455b-90d4-c44d94c7388d_1650169239.717305.png)\n\nWe only need to save the number of two factors: `2, 5`. It\'s quite straightforward to get the number of trailing zeros from these numbers of factors.\n\n![image](https://assets.leetcode.com/users/images/2f0cb380-ecfa-4364-b91b-30f43ed6f418_1650170082.105669.png)\n\n> In case I didn\'t explain it well.\n> `a->[2\'s, 5\'s]`\n> 40 = 2\\*2\\*2\\*5, three 2\'s and one 5, thus we let `40=[3,1]`\n> Similarly, `23=[0,0]` since it has no factor as 2 or 5, \n> `1500=3\\*2\\*2\\*5\\*5\\*5`, thus we let `1500=[2,3]` \n> Hence, `40*23*1500` has `[5,4]` which makes 4 trailing zeros.\n\n**complexity**\nTime: O(n)\nSpace: O(n)\n\n**code**\n```\nclass Solution:\n    def maxTrailingZeros(self, A: List[List[int]]) -> int:\n        m, n = len(A), len(A[0])\n        left = [[[0, 0] for _ in range(n)] for _ in range(m)]\n        top = [[[0, 0] for _ in range(n)] for _ in range(m)]\n        \n        def helper(num):\n            a, b = 0, 0\n            while num % 2 == 0:\n                num //= 2\n                a += 1\n            while num % 5 == 0:\n                num //= 5\n                b += 1\n            return [a, b]\n        \n        for i in range(m):\n            for j in range(n):\n                if j == 0:\n                    left[i][j] = helper(A[i][j])\n                else:\n                    a, b = helper(A[i][j])\n                    left[i][j][0] = left[i][j - 1][0] + a\n                    left[i][j][1] = left[i][j - 1][1] + b\n        for j in range(n):\n            for i in range(m):\n                if i == 0:\n                    top[i][j] = helper(A[i][j])\n                else:\n                    a, b, = helper(A[i][j])\n                    top[i][j][0] = top[i - 1][j][0] + a               \n                    top[i][j][1] = top[i - 1][j][1] + b\n\n\n        ans = 0\n        for i in range(m):\n            for j in range(n):\n                a, b = top[m - 1][j]\n                d, e= left[i][n - 1]\n                x, y = helper(A[i][j])\n                a1, b1 = top[i][j]\n                a2, b2= left[i][j]\n                tmp = [a1 + a2 - x, b1 + b2 - y]\n                ans = max(ans, min(tmp))\n                tmp = [d - a2 + a1, e - b2 + b1]\n                ans = max(ans, min(tmp))             \n                tmp = [a - a1 + a2, b - b1 + b2]\n                ans = max(ans, min(tmp))\n                tmp = [a + d - a1 - a2 + x, b + e - b1 - b2 + y]\n                ans = max(ans, min(tmp))\n                \n        return ans\n```\n
[]