Easy to Understand C++ solution
\nclass Solution {\npublic:\n    int maxCompatibilitySum(vector<vector<int>>& students, vector<vector<int>>& mentors) {\n        \n        vector<int> stuMask;\
```\nclass Solution {\npublic:\n    int maxCompatibilitySum(vector<vector<int>>& students, vector<vector<int>>& mentors) {\n        \n        vector<int> stuMask;\n        vector<int> menMask;\n        \n        for(int i=0;i<students.size();i++){\n            int mask=0;\n            for(int j=0;j<students[i].size();j++){\n                if(students[i][j]==1)\n                    mask=mask|(1<<j);\n            }\n            stuMask.push_back(mask);\n        }\n        \n        for(int i=0;i<mentors.size();i++){\n            int mask=0;\n            for(int j=0;j<mentors[i].size();j++){\n                if(mentors[i][j]==1)\n                    mask=mask|(1<<j);\n            }\n            menMask.push_back(mask);\n        }\n        \n        vector<vector<int>> dp(students.size(),vector<int>(mentors.size(),0));\n        for(int i=0;i<stuMask.size();i++){\n            for(int j=0;j<menMask.size();j++){\n                int val=0;\n                for(int k=0;k<students[0].size();k++){\n                    if((stuMask[i]&(1<<k)) == (menMask[j]&(1<<k))){\n                        val++;\n                    }\n                }\n                dp[i][j]=val;\n            }\n        }\n        \n        int ans=0;\n        vector<bool> stuVisited(students.size(),false);\n        helper(dp,ans,0,stuVisited,0);\n        return ans;\n    }\n    \n    void helper(vector<vector<int>> &dp,int &ans,int val,vector<bool> &stuVisited,int idx){\n        \n        if(idx==stuVisited.size()){\n            ans=max(ans,val);\n            return;\n        }\n        \n        for(int i=0;i<stuVisited.size();i++){\n            if(!stuVisited[i]){\n                stuVisited[i]=true;\n                helper(dp,ans,val+dp[i][idx],stuVisited,idx+1);\n                stuVisited[i]=false;\n            }\n        }\n    }\n};\n```
[]