<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:
        print('new')
        m, n = len(grid), len(grid[0])
        memo = [[[0, 0] for j in range(n)] for i in range(m)]
        l2r = [[[0, 0] for j in range(n)] for i in range(m)]
        r2l = [[[0, 0] for j in range(n)] for i in range(m)]
        t2b = [[[0, 0] for j in range(n)] for i in range(m)]
        b2t = [[[0, 0] for j in range(n)] for i in range(m)]
        for i in range(m):
            for j in range(n):
                t, t_cnt = 2, 0
                while grid[i][j] % t == 0:
                    t <<= 1
                    t_cnt += 1
                f, f_cnt = 5, 0
                while grid[i][j] % f == 0:
                    f *= 5
                    f_cnt += 1
                memo[i][j] = [t_cnt, f_cnt]
        for i in range(m):
            l2r[i][0] = memo[i][0]
            r2l[i][n - 1] = memo[i][n - 1]
        for j in range(n):
            t2b[0][j] = memo[0][j]
            b2t[m - 1][j] = memo[m - 1][j]
        for i in range(m):
            for j in range(1, n):
                l2r[i][j][0] = memo[i][j][0] + l2r[i][j - 1][0]
                l2r[i][j][1] = memo[i][j][1] + l2r[i][j - 1][1]
            for j in range(n - 2, -1, -1):
                r2l[i][j][0] = memo[i][j][0] + r2l[i][j + 1][0]
                r2l[i][j][1] = memo[i][j][1] + r2l[i][j + 1][1]
        for j in range(n):
            for i in range(1, m):
                t2b[i][j][0] = memo[i][j][0] + t2b[i - 1][j][0]
                t2b[i][j][1] = memo[i][j][1] + t2b[i - 1][j][1]
            for i in range(m - 2, -1, -1):
                b2t[i][j][0] = memo[i][j][0] + b2t[i + 1][j][0]
                b2t[i][j][1] = memo[i][j][1] + b2t[i + 1][j][1]
        zeros = 0
        for i in range(m):
            for j in range(n):
                twos, fives = memo[i][j]
                l_twos = 0 if (j == 0) else l2r[i][j - 1][0]
                l_fives = 0 if (j == 0) else l2r[i][j - 1][1]
                r_twos = 0 if (j == n - 1) else r2l[i][j + 1][0]
                r_fives = 0 if (j == n - 1) else r2l[i][j + 1][1]
                t_twos = 0 if (i == 0) else t2b[i - 1][j][0]
                t_fives = 0 if (i == 0) else t2b[i - 1][j][1]
                b_twos = 0 if (i == m - 1) else b2t[i + 1][j][0]
                b_fives = 0 if (i == m - 1) else b2t[i + 1][j][1]
                lst = [[l_twos, l_fives], [r_twos, r_fives], [t_twos, t_fives], [b_twos, b_fives]]
                for p in range(4):
                    for q in range(4):
                        if p != q:
                            zeros = max(zeros, min(twos + lst[p][0] + lst[q][0], fives + lst[p][1] + lst[q][1]))
        return zeros
134300