Java prefix sum solution
\nclass Solution {\n    public int maxTrailingZeros(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int[][][] dph = new 
```\nclass Solution {\n    public int maxTrailingZeros(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n        int[][][] dph = new int[m][n][3];\n        int[][][] dpv = new int[m][n][3];\n        int hmax0 = 0;\n        int vmax0 = 0;\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                int curr = grid[i][j];\n                int two = 0;\n                int five = 0;\n                if (j >= 1) {\n                    two = dph[i][j-1][1];\n                    five = dph[i][j-1][2];\n                }\n                while (curr > 0 && curr % 2 == 0) {\n                    two++;\n                    curr /= 2;\n                }\n                while (curr > 0 && curr % 5 == 0) {\n                    five++;\n                    curr /= 5;\n                }\n                dph[i][j][1] = two;\n                dph[i][j][2] = five;\n                dph[i][j][0] = Math.min(dph[i][j][1], dph[i][j][2]);\n            }\n            hmax0 = Math.max(hmax0, dph[i][n-1][0]);\n        }\n        \n        for (int j = 0; j < n; j++) {\n            for (int i = 0; i < m; i++) {\n                int curr = grid[i][j];\n                int two = 0;\n                int five = 0;\n                if (i >= 1) {\n                    two = dpv[i-1][j][1];\n                    five = dpv[i-1][j][2];\n                }\n                while (curr > 0 && curr % 2 == 0) {\n                    two++;\n                    curr /= 2;\n                }\n                while (curr > 0 && curr % 5 == 0) {\n                    five++;\n                    curr /= 5;\n                }\n                dpv[i][j][1] = two;\n                dpv[i][j][2] = five;\n                dpv[i][j][0] = Math.min(dpv[i][j][1], dpv[i][j][2]);\n            }\n            vmax0 = Math.max(vmax0, dpv[m-1][j][0]);\n        }\n\t\t\n        int res = Math.max(vmax0, hmax0);\n\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                int otwo = dph[i][j][1];\n                int ofive = dph[i][j][2];\n                \n                int res1 = 0;\n                if (i >= 1) {\n                    int ntwo = otwo + dpv[i-1][j][1];\n                    int nfive = ofive + dpv[i-1][j][2];\n                    res1 = Math.min(ntwo, nfive);\n                }\n                \n                int res2 = 0;\n                if (i < m - 1) {\n                    int ntwo = otwo + dpv[m-1][j][1] - dpv[i][j][1];\n                    int nfive = ofive + dpv[m-1][j][2] - dpv[i][j][2];\n                    res2 = Math.min(ntwo, nfive);\n                }\n                res = Math.max(res, res1);\n                res = Math.max(res, res2);\n            }\n\t\t\t\n\t\t\tfor (int j = n - 1; j >= 0; j--) {\n                int otwo = 0;\n                int ofive = 0;\n                if (j >= 1) {\n                    otwo = dph[i][n-1][1] - dph[i][j-1][1];\n                    ofive = dph[i][n-1][2] - dph[i][j-1][2];\n                } else {\n                    otwo = dph[i][n-1][1];\n                    ofive = dph[i][n-1][2];\n                }\n                \n                int res1 = 0;\n                if (i >= 1) {\n                    int ntwo = otwo + dpv[i-1][j][1];\n                    int nfive = ofive + dpv[i-1][j][2];\n                    res1 = Math.min(ntwo, nfive);\n                }\n                \n                int res2 = 0;\n                if (i < m - 1) {\n                    int ntwo = otwo + dpv[m-1][j][1] - dpv[i][j][1];\n                    int nfive = ofive + dpv[m-1][j][2] - dpv[i][j][2];\n                    res2 = Math.min(ntwo, nfive);\n                }\n                \n                res = Math.max(res, res1);\n                res = Math.max(res, res2);\n            }\n        }\n\t\t\n        return res;\n    }\n}\n```
['Java']