With Bitmasking
\nclass Solution {\n    int dp[10][1<<10];\npublic:\n    int check(vector<vector<int>>&st,vector<vector<int>>&mt,int i,int j)\n    {\n        int ans=0;\n      
```\nclass Solution {\n    int dp[10][1<<10];\npublic:\n    int check(vector<vector<int>>&st,vector<vector<int>>&mt,int i,int j)\n    {\n        int ans=0;\n        \n        for(int k=0;k<st[i].size();k++)\n        {\n            if(st[i][k]==mt[j][k])\n            {\n                ans++;\n            }\n        }\n        return ans;\n    }\n    int get(vector<vector<int>>&st,vector<vector<int>>&mt,int index,int mask)\n    {\n        if(mask==0||(index>st.size()))\n        {\n            return 0;\n        }\n        if(dp[index][mask]!=-1)\n        {\n            return dp[index][mask];\n        }\n        int ans=0;\n        for(int i=0;i<mt.size();i++)\n        {\n            if(mask&(1<<i))\n            {\n                int res=check(st,mt,index,i)+get(st,mt,index+1,mask^(1<<i));\n                ans=max(ans,res);\n            }\n        }\n        return dp[index][mask]=ans;\n    }\n    int maxCompatibilitySum(vector<vector<int>>& st, vector<vector<int>>& me) {\n        \n        int n=st.size();\n        int m=me.size();\n        int mask=(1<<m)-1;\n        memset(dp,-1,sizeof(dp));\n        return get(st,me,0,mask);\n    }\n};\n```
[]