C++ solution
\nclass Solution {\npublic: \n    vector<int> nextSmallerElement(int *arr,int n)\n    {\n        stack<int>s;\n        s.push(-1);\n        vector<int> ans(n);\
```\nclass Solution {\npublic: \n    vector<int> nextSmallerElement(int *arr,int n)\n    {\n        stack<int>s;\n        s.push(-1);\n        vector<int> ans(n);\n        for(int i=n-1;i>=0;i--)\n        {\n            int curr=arr[i];\n            while(s.top()!=-1 && arr[s.top()]>=curr)\n            {\n                s.pop();\n            }\n            ans[i]=s.top();\n            s.push(i);\n        }\n        return ans;\n    }\n    vector<int> prevSmallerElement(int *arr,int n)\n    {\n        stack<int>s;\n        s.push(-1);\n        vector<int> ans(n);\n        for(int i=0;i<n;i++)\n        {\n            int curr=arr[i];\n            while(s.top()!=-1 && arr[s.top()]>=curr)\n            {\n                s.pop();\n            }\n            ans[i]=s.top();\n            s.push(i);\n        }\n        return ans;\n    }\n    \n    int largestRectangleArea(int *heights,int n) {\n        //int n=heights.size();\n        \n        vector<int> next(n);\n        next=nextSmallerElement(heights,n);\n        \n        vector<int> prev(n);\n        prev=prevSmallerElement(heights,n);\n        \n        int area=INT_MIN;\n        for(int i=0;i<n;i++)\n        {\n            int l=heights[i];\n            \n            if(next[i]==-1)\n                next[i]=n;\n            int b=next[i]-prev[i]-1;\n            int newArea=l*b;\n            area= max(area,newArea);\n        }\n        return area;\n        \n    }\n    \n    int maximalRectangle(vector<vector<char>>& matrix) {\n        int rows=matrix.size();\n        int cols=matrix[0].size();\n        int mat[rows][cols];\n        for(int i=0;i<rows;i++)\n        {\n            for(int j=0;j<cols;j++)\n            {\n                mat[i][j]=matrix[i][j]-\'0\';\n            }\n        }\n        int area=largestRectangleArea(mat[0],cols);\n        for(int i=1;i<rows;i++)\n        {\n            for(int j=0;j<cols;j++)\n            {\n               if(mat[i][j]!=0)\n                   mat[i][j]=mat[i][j]+mat[i-1][j];\n                else\n                    mat[i][j]=0;\n            }\n             int newArea=largestRectangleArea(mat[i],cols);\n            area=max(area,newArea);\n        }\n        \n       return area;\n    }\n};\n```
[]