JAVA
\nclass Solution {\n    int max=0;\n    Set<Integer>set=new HashSet<>();\n    public int maxCompatibilitySum(int[][] students, int[][] mentors) {\n        \n   
```\nclass Solution {\n    int max=0;\n    Set<Integer>set=new HashSet<>();\n    public int maxCompatibilitySum(int[][] students, int[][] mentors) {\n        \n        solve(students,mentors,"");\n        return max;\n    }\n    \n    void solve(int[][]students,int[][]mentor,String s){\n\t\n\t//find the ans from each permutation and return the ,max answer\n        if(students.length==set.size()){\n            int sum=0;\n            for(int i=0;i<students.length;i++){\n                \n                int p=0;\n                int[]z=students[i];\n                int[]z1=mentor[s.charAt(i)-\'0\'];\n                \n                for(int j=0;j<z.length;j++){\n                    if(z[j]==z1[j]){\n                        p++;\n                    }\n                }\n                sum=sum+p;\n            }\n            max=Math.max(max,sum);\n            return;\n        }\n\t\t// Calculate all permutations\n        for(int i=0;i<students.length;i++){\n            if(set.contains(i)){\n                continue;\n            }\n            set.add(i);\n            solve(students,mentor,s+i);\n            set.remove(i);\n        }\n        \n    }\n}\n```
[]