Python 3   || w/ some explanation   ||   T/S: 89% / 89%
Here\'s the plan:\n\n- We construct a prefix sum of 4-tuples, writing overgridas we go. For each cell, we determine (up2, up5, left2, right5), the accummulated 
Here\'s the plan:\n\n- We construct a prefix sum of 4-tuples, writing over`grid`as we go. For each cell, we determine `(up2, up5, left2, right5)`, the accummulated factors of two and five for the up-direction and the left-direction respectively.\n\n- We use the transformed`grid`to determine for each cell the count of zeros over the four paths: up-left, down-left, up-right, down-right.\n- We determine the max zeros for each cell along the four paths, and then determine the overall max from those cell maxs.\n```\nclass Solution:\n    def maxTrailingZeros(self, grid: list[list[int]]) -> int:\n\n        m, n = len(grid)+1, len(grid[0])+1\n        grid = [[(0,0,0,0)]*n]+[[(0,0,0,0)]+row for row in grid]\n\n        def pref(row: int,col: int)-> tuple:        # <-- prefix for each cell\n        \n            val = grid[row][col]\n            for f2 in range(19):\n                if val%2: break\n                val//= 2\n   \n            for f5 in range(6):\n                if val%5: break\n                val//= 5\n        \n            (u2, u5, _,_), (_,_, l2, l5) = grid[row-1][col], grid[row][col-1]\n            return (f2 + u2, f5 + u5, f2 + l2, f5 + l5)\n        \n        def countZeros(r: int,c: int)-> int:        #  <--Count the zeros    \n            up2   ,up5    = grid[r][c][0],grid[r][c][1]\n            down2 ,down5  = grid[m-1][c][0]-grid[r-1][c][0],grid[m-1][c][1]-grid[r-1][c][1]\n            \n            left2 ,left5  = grid[r][c-1][2],grid[r][c-1][3]\n            right2,right5 = grid[r][n-1][2]-grid[r][c][2],grid[r][n-1][3]-grid[r][c][3] \n\n            return max(min(up2+left2 ,up5+left5 ), min(down2+left2 ,down5+left5 ),\n                       min(up2+right2,up5+right5), min(down2+right2,down5+right5))\n\n        for r in range(1,m):\n            for c in range(1,n):grid[r][c] = pref(r,c)\n\n        return max(countZeros(r,c) for c in range(1,n) for r in range(1,m))\n```\n[https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/submissions/1286570286/](https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/submissions/1286570286/)\n\nI could be wrong, but I think that time complexity is *O*(*N*) (wnere*N* is *mn* and space complexity is probably *O*(*N*) but maybe *O*(1). I\'m just not sure.\n\nI could be wrong, but I think that time complexity is *O*(*N*) and space complexity is *O*(*N*), in which *N* ~ `m*n`.\n
['Python3']