[Python] Iterative. Prefix Sum + Factor caching Beats 100% 2601 ms
\nclass Solution(object):\n    def maxTrailingZeros(self, grid):\n        m = len(grid)\n        n = len(grid[0])\n        factors = {}\n        for val in rang
```\nclass Solution(object):\n    def maxTrailingZeros(self, grid):\n        m = len(grid)\n        n = len(grid[0])\n        factors = {}\n        for val in range(1, 1001):\n            num = val\n            cnt2, cnt5 = 0, 0\n            while num > 0 and num % 2 == 0: \n                cnt2 += 1\n                num = num / 2\n            while num > 0 and num % 5 == 0: \n                cnt5 += 1\n                num = num / 5\n            factors[val] = (cnt2, cnt5)\n        row2, row5, col2, col5 = [], [], [], []\n        for i in range(m + 1): \n            row2.append([0]*(n + 1))\n            col2.append([0]*(n + 1))\n            row5.append([0]*(n + 1))\n            col5.append([0]*(n + 1))\n        for i in range(1, m + 1):\n            for j in range(1, n + 1):\n                cnt2, cnt5 = factors[grid[i - 1][j - 1]]\n                row2[i][j] = cnt2 + row2[i][j - 1]\n                row5[i][j] = cnt5 + row5[i][j - 1]\n                col2[i][j] = cnt2 + col2[i - 1][j]\n                col5[i][j] = cnt5 + col5[i - 1][j]\n        res = 0\n        for i in range(1, m + 1):\n            for j in range(1, n + 1):\n                cnt2, cnt5 = factors[grid[i - 1][j - 1]]\n                top2, top5 = col2[i-1][j], col5[i-1][j]\n                bottom2, bottom5 = col2[m][j] - col2[i][j], col5[m][j] - col5[i][j]\n                left2, left5 = row2[i][j-1], row5[i][j-1]\n                right2, right5 = row2[i][n] - row2[i][j], row5[i][n] - row5[i][j]\n                res = max(res, min(top2 + left2 + cnt2, top5 + left5 + cnt5), min(top2 + right2 + cnt2, top5 + right5 + cnt5), min(bottom2 + left2 + cnt2, bottom5 + left5 + cnt5), min(bottom2 + right2 + cnt2, bottom5 + right5 + cnt5))\n        return res\n                \n                \n                \n            \n        \n```
[]