<p>You are given a 2D integer array <code>grid</code> of size <code>m x n</code>, where each cell contains a positive integer.</p>

<p>A <strong>cornered path</strong> is defined as a set of adjacent cells with <strong>at most</strong> one turn. More specifically, the path should exclusively move either <strong>horizontally</strong> or <strong>vertically</strong> up to the turn (if there is one), without returning to a previously visited cell. After the turn, the path will then move exclusively in the <strong>alternate</strong> direction: move vertically if it moved horizontally, and vice versa, also without returning to a previously visited cell.</p>

<p>The <strong>product</strong> of a path is defined as the product of all the values in the path.</p>

<p>Return <em>the <strong>maximum</strong> number of <strong>trailing zeros</strong> in the product of a cornered path found in </em><code>grid</code>.</p>

<p>Note:</p>

<ul>
	<li><strong>Horizontal</strong> movement means moving in either the left or right direction.</li>
	<li><strong>Vertical</strong> movement means moving in either the up or down direction.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/23/ex1new2.jpg" style="width: 577px; height: 190px;" />
<pre>
<strong>Input:</strong> grid = [[23,17,15,3,20],[8,1,20,27,11],[9,4,6,2,21],[40,9,1,10,6],[22,7,4,5,3]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The grid on the left shows a valid cornered path.
It has a product of 15 * 20 * 6 * 1 * 10 = 18000 which has 3 trailing zeros.
It can be shown that this is the maximum trailing zeros in the product of a cornered path.

The grid in the middle is not a cornered path as it has more than one turn.
The grid on the right is not a cornered path as it requires a return to a previously visited cell.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/ex2.jpg" style="width: 150px; height: 157px;" />
<pre>
<strong>Input:</strong> grid = [[4,3,2],[7,6,1],[8,8,8]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The grid is shown in the figure above.
There are no cornered paths in the grid that result in a product with a trailing zero.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= grid[i][j] &lt;= 1000</code></li>
</ul>

class Solution:
    def maxTrailingZeros(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        
        # Function to count the number of 2s and 5s in a number
        def count_factors(num):
            two_count = 0
            five_count = 0
            while num % 2 == 0:
                num //= 2
                two_count += 1
            while num % 5 == 0:
                num //= 5
                five_count += 1
            return two_count, five_count
        
        # Initialize prefix and suffix sums for 2s and 5s
        prefix_two = [[0] * (n + 1) for _ in range(m)]
        prefix_five = [[0] * (n + 1) for _ in range(m)]
        suffix_two = [[0] * (n + 1) for _ in range(m)]
        suffix_five = [[0] * (n + 1) for _ in range(m)]
        
        for i in range(m):
            for j in range(n):
                two_count, five_count = count_factors(grid[i][j])
                prefix_two[i][j + 1] = prefix_two[i][j] + two_count
                prefix_five[i][j + 1] = prefix_five[i][j] + five_count
        
        for i in range(m):
            for j in range(n - 1, -1, -1):
                two_count, five_count = count_factors(grid[i][j])
                suffix_two[i][j] = suffix_two[i][j + 1] + two_count
                suffix_five[i][j] = suffix_five[i][j + 1] + five_count
        
        # Initialize prefix and suffix sums for 2s and 5s
        prefix_col_two = [[0] * n for _ in range(m + 1)]
        prefix_col_five = [[0] * n for _ in range(m + 1)]
        suffix_col_two = [[0] * n for _ in range(m + 1)]
        suffix_col_five = [[0] * n for _ in range(m + 1)]
        
        for j in range(n):
            for i in range(m):
                two_count, five_count = count_factors(grid[i][j])
                prefix_col_two[i + 1][j] = prefix_col_two[i][j] + two_count
                prefix_col_five[i + 1][j] = prefix_col_five[i][j] + five_count
        
        for j in range(n):
            for i in range(m - 1, -1, -1):
                two_count, five_count = count_factors(grid[i][j])
                suffix_col_two[i][j] = suffix_col_two[i + 1][j] + two_count
                suffix_col_five[i][j] = suffix_col_five[i + 1][j] + five_count
        
        # Calculate the maximum number of trailing zeros
        max_zeros = 0
        for i in range(m):
            for j in range(n):
                left_two = prefix_two[i][j + 1]
                left_five = prefix_five[i][j + 1]
                right_two = suffix_two[i][j]
                right_five = suffix_five[i][j]
                up_two = prefix_col_two[i + 1][j]
                up_five = prefix_col_five[i + 1][j]
                down_two = suffix_col_two[i][j]
                down_five = suffix_col_five[i][j]
                
                # Calculate the number of 2s and 5s in each possible cornered path
                path1_two = left_two + up_two - count_factors(grid[i][j])[0]
                path1_five = left_five + up_five - count_factors(grid[i][j])[1]
                
                path2_two = left_two + down_two - count_factors(grid[i][j])[0]
                path2_five = left_five + down_five - count_factors(grid[i][j])[1]
                
                path3_two = right_two + up_two - count_factors(grid[i][j])[0]
                path3_five = right_five + up_five - count_factors(grid[i][j])[1]
                
                path4_two = right_two + down_two - count_factors(grid[i][j])[0]
                path4_five = right_five + down_five - count_factors(grid[i][j])[1]
                
                # Update the maximum number of trailing zeros
                max_zeros = max(max_zeros, min(path1_two, path1_five))
                max_zeros = max(max_zeros, min(path2_two, path2_five))
                max_zeros = max(max_zeros, min(path3_two, path3_five))
                max_zeros = max(max_zeros, min(path4_two, path4_five))
        
        return max_zeros

# Example usage:
solution = Solution()
grid = [[23,17,15,3,20],[8,1,20,27,11],[9,4,6,2,21],[40,9,1,10,6],[22,7,4,5,3]]
print(solution.maxTrailingZeros(grid))  # Output: 3
5024