C++ || EASY AND CLEAR TO UNDERSTAND || Simple Prefix Sum :)
Approach\nmake 4 arrays top, down, up and left and store occurances of 2s and 5s in them with pair \nfor each index try to find by which pair we got maximum tra
# Approach\nmake 4 arrays top, down, up and left and store occurances of 2s and 5s in them with pair \nfor each index try to find by which pair we got maximum trailing zeroes.\n\n# Code\n```\nclass Solution {\npublic:\n    \n    pair<int, int> countTwosAndFives(int num) {\n        int two = 0, five = 0;\n        while(num % 5 == 0) {\n            five++;\n            num /= 5;\n        }\n        \n        while(num % 2 == 0) {\n            two++;\n            num /= 2;\n        }\n        \n        return {two, five};\n    }\n    \n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int n = grid.size();\n        int m = grid[0].size();\n        \n        vector<vector<pair<int, int>>> left(n, vector<pair<int, int>> (m));\n        vector<vector<pair<int, int>>> right(n, vector<pair<int, int>> (m));\n        vector<vector<pair<int, int>>> up(n, vector<pair<int, int>> (m));\n        vector<vector<pair<int, int>>> down(n, vector<pair<int, int>> (m));\n        \n        //   left matrix...\n        for(int i=0; i<n; i++) {\n            int two = 0, five = 0;\n            for(int j=0; j<m; j++) {\n                left[i][j] = {two, five};\n                pair<int,int> res = countTwosAndFives(grid[i][j]);\n                two += res.first;\n                five += res.second;\n            }\n        }\n        \n        //   right matrix...\n        for(int i=0; i<n; i++) {\n            int two = 0, five = 0;\n            for(int j=m-1; j>=0; j--) {\n                right[i][j] = {two, five};\n                pair<int,int> res = countTwosAndFives(grid[i][j]);\n                two += res.first;\n                five += res.second;\n            }\n        }\n         \n        //   up matrix...\n        for(int i=0; i<m; i++) {\n            int two = 0, five = 0;\n            for(int j=0; j<n; j++) {\n                up[j][i] = {two, five};\n                pair<int,int> res = countTwosAndFives(grid[j][i]);\n                two += res.first;\n                five += res.second;\n            }\n        }\n        \n        //   down matrix...\n        for(int i=0; i<m; i++) {\n            int two = 0, five = 0;\n            for(int j=n-1; j>=0; j--) {\n                down[j][i] = {two, five};\n                pair<int,int> res = countTwosAndFives(grid[j][i]);\n                two += res.first;\n                five += res.second;\n            }\n        }\n        \n        \n        \n        int ans = 0;\n        for(int i=0; i<n; i++) {\n            for(int j=0; j<m; j++) {\n                pair<int,int> res = countTwosAndFives(grid[i][j]);\n                ans = max(ans, min(left[i][j].first + up[i][j].first + res.first, \n                                   left[i][j].second + up[i][j].second + res.second));\n                ans = max(ans, min(left[i][j].first + down[i][j].first + res.first, \n                                   left[i][j].second + down[i][j].second + res.second));\n                ans = max(ans, min(right[i][j].first + up[i][j].first + res.first, \n                                   right[i][j].second + up[i][j].second + res.second));\n                ans = max(ans, min(right[i][j].first + down[i][j].first + res.first, \n                                   right[i][j].second + down[i][j].second + res.second));\n            }\n        }\n        return ans;\n    }\n};\n```
['Prefix Sum', 'C++']