Java
\nclass Solution {\n    public int maxTrailingZeros(int[][] grid) {\n        int m = grid.length, n = grid[0].length, max = 0;\n        int[][] row2 = new int[m
```\nclass Solution {\n    public int maxTrailingZeros(int[][] grid) {\n        int m = grid.length, n = grid[0].length, max = 0;\n        int[][] row2 = new int[m+1][n+1];\n        int[][] row5 = new int[m+1][n+1];\n        int[][] col2 = new int[m+1][n+1];\n        int[][] col5 = new int[m+1][n+1];\n        int[][] factor2 = new int[m][n];\n        int[][] factor5 = new int[m][n];\n        \n        for (int r=0; r<m; r++) {\n            for (int c=0; c<n; c++) {\n                int factorTwo = findFactor(grid[r][c], 2);\n                int factorFive = findFactor(grid[r][c], 5);\n                \n                row2[r+1][c+1] = row2[r+1][c] + factorTwo;\n                row5[r+1][c+1] = row5[r+1][c] + factorFive;\n                \n                col2[r+1][c+1] = col2[r][c+1] + factorTwo;\n                col5[r+1][c+1] = col5[r][c+1] + factorFive;\n                \n                factor2[r][c] = factorTwo;\n                factor5[r][c] = factorFive;\n            }\n        }\n        \n        for (int r=0; r<m; r++) {\n            for (int c=0; c<n; c++) {\n                int cur2 = factor2[r][c];\n                int cur5 = factor5[r][c];\n                \n                int up2 = col2[r+1][c+1];\n                int up5 = col5[r+1][c+1];\n                \n                int down2 = col2[m][c+1] - col2[r][c+1];\n                int down5 = col5[m][c+1] - col5[r][c+1];\n                \n                int left2 = row2[r+1][c+1];\n                int left5 = row5[r+1][c+1];\n                \n                int right2 = row2[r+1][n] - row2[r+1][c];\n                int right5 = row5[r+1][n] - row5[r+1][c];\n                \n                max = Math.max(max, Math.min(left2 + up2 - cur2, left5 + up5 - cur5));\n                max = Math.max(max, Math.min(right2 + up2 - cur2, right5 + up5 - cur5));\n                max = Math.max(max, Math.min(left2 + down2 - cur2, left5 + down5 - cur5));\n                max = Math.max(max, Math.min(right2 + down2 - cur2, right5 + down5 - cur5));\n            }\n        }\n        \n        return max;\n    }\n    \n    private int findFactor(int a, int b) {\n        int factors = 0;\n        while (a % b == 0) {\n            a /= b;\n            factors++;\n        }\n        return factors;\n    }\n}\n```
[]