O(1)-space implementation (except stack space)
I am spawning 3-directional (it is interesting to see that they need not be 4-directional i.e; go up, down, left, and right, three directions are sufficient for
I am spawning 3-directional (it is interesting to see that they need not be 4-directional i.e; go up, down, left, and right, three directions are sufficient for spanning the array) crawlers at promising (cells which are already multiples of either 2 or 5) roots.\n\nUsing marking-unmarking technique to avoid a visited array.\n```\n\tvoid rec(vector<vector<int> >& grid, int& ans, int num_twos, int num_fives, int i, int j, bool turned, int dir)\n    {\n        if(grid[i][j]==-1)\n            return;\n        // there is a more optimal way but it won\'t matter here since max(grid[i][j]) is 1000. so that\'s a max of 9 iterations for twos and 4 for fives in the following\n        int twos = 0, fives = 0, temp = grid[i][j], n = grid[i][j], num_zeroes;\n        while(!(n&1))\n        {\n            ++twos;\n            n /= 2;\n        }\n        num_twos += twos;\n        n = grid[i][j];\n        while(n%5==0)\n        {\n            ++fives;\n            n /= 5;\n        }\n        num_fives += fives;\n        grid[i][j] = -1;\n        num_zeroes = (num_twos<num_fives) ? num_twos : num_fives;\n        if(ans<num_zeroes)\n            ans = num_zeroes;\n        // can proceed virgin\n        if(dir==1 && i+1<grid.size())\n            rec(grid, ans, num_twos, num_fives, i+1, j, turned, 1);\n        else if(dir==2 && i-1>=0)\n            rec(grid, ans, num_twos, num_fives, i-1, j, turned, 2);\n        //else if(dir==3 && j+1<grid[0].size())\n            //rec(grid, ans, num_twos, num_fives, i, j+1, turned, 3);\n        else if(dir==4 && j-1>=0)\n            rec(grid, ans, num_twos, num_fives, i, j-1, turned, 4);\n        // or pivot\n        if(!turned)\n        {\n            if(dir==1 || dir==2)\n            {\n            //rec(grid, ans, num_twos, num_fives, i, j+1, true, 3);\n            if(j-1>=0)\n                rec(grid, ans, num_twos, num_fives, i, j-1, true, 4);\n            }\n            else\n            {\n            if(i+1<grid.size())\n                rec(grid, ans, num_twos, num_fives, i+1, j, true, 1);\n            if(i-1>=0)\n                rec(grid, ans, num_twos, num_fives, i-1, j, true, 2);\n            }\n        }\n        grid[i][j] = temp;\n    }\n    \n    int maxTrailingZeros(vector<vector<int>>& grid) {\n        int ans = 0;\n        for(int i=0; i<grid.size(); ++i)\n            for(int j=0; j<grid[0].size(); ++j)\n            {\n                if(!(grid[i][j]&1) || grid[i][j]%5==0) // only non empty roots\n                {\n                rec(grid, ans, 0, 0, i, j, false, 1); // i+1\n                rec(grid, ans, 0, 0, i, j, false, 2); // i-1\n                //rec(grid, ans, 0, 0, i, j, false, 3); // j+1\n                rec(grid, ans, 0, 0, i, j, false, 4); // j-1   \n                }\n            }\n        return ans;\n    }\n```
['Recursion']