Java | Rotate 3 times
It is quite apparent it is a prefix sum question but geez, it is really implementation heavy due to \n1. We have to keep track of both 2 and 5\'s prefix sum in 
It is quite apparent it is a prefix sum question but geez, it is **really** implementation heavy due to \n1. We have to keep track of both 2 and 5\'s prefix sum in a 2D grid\n\n2. There are like, a lot of directions, to consider. That is, start from horizontal vs vertical, then go up vs down or right vs left.\n\nI missed considering a direction during contest and submission failed at the last 5 test cases. Troubleshoot was kind of difficult. Annoyed. \nHere comes an idea that didn\'t occur to me during contest: To deal with this annoyance, we can literally just **rotate the grid 3 times**. \n\n```Java\nclass Solution {\n    public int maxTrailingZeros(int[][] grid) {\n        int ans = 0;\n        for (int i = 0; i < 4; i++){\n            ans = Math.max(ans, solve(grid));\n            grid = rotate(grid);\n        }\n        return ans;\n    }\n\n    private int solve(int[][] grid){\n        int n = grid[0].length, m = grid.length, ans = 0;\n        int[][] fcol = new int[m + 1][n];\n        int[][] tcol = new int[m + 1][n];\n        int[][] frow = new int[n + 1][m];\n        int[][] trow = new int[n + 1][m];\n        for (int i = 0; i < m; i++){\n            for (int j = 0; j < n; j++){\n                int t = count(grid[i][j], 2);\n                int f = count(grid[i][j], 5);\n                fcol[i + 1][j] += fcol[i][j] + f;\n                tcol[i + 1][j] += tcol[i][j] + t;\n                frow[j + 1][i] += frow[j][i] + f;\n                trow[j + 1][i] += trow[j][i] + t;\n            }\n        }\n        for (int i = 0; i < m; i++){\n            for (int j = 0; j < n; j++){\n                int rowTwo = trow[j + 1][i];\n                int rowFive= frow[j + 1][i];\n                int colTwo = tcol[m][j] - tcol[i + 1][j];\n                int colFive= fcol[m][j] - fcol[i + 1][j];\n                ans = Math.max(ans, Math.min(rowTwo + colTwo, rowFive + colFive));\n            }\n        }\n\n        return ans;\n    }\n\n    private int count(int n, int w){\n        int count = 0;\n        while(n % w == 0){\n            count++;\n            n /= w;\n        }\n        return count;\n    }\n\n    private int[][] rotate(int[][] grid){\n        int n = grid[0].length, m = grid.length;\n        int[][] next = new int[n][m];\n        for (int i = 0; i < m; i++){\n            for (int j = 0; j < n; j++){\n                next[j][m - 1 - i] = grid[i][j];\n            }\n        }\n        return next;\n    }\n}\n```
['Java']