JAVA
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    List<List<Integer>> a;\n    int[][] dp;\n    public int calc(int n, int mask) {\n        if (n == 0) return 0;\n        if (dp[n][mask] != -1) return dp[n][mask];\n        int ans = calc(n - 1, mask);\n        for (int x : a.get(n)) {\n            if ((mask & (1 << x)) != 0) continue;\n            ans = Math.max(ans, n + calc(n - 1, mask | (1 << x)));\n        }\n        return dp[n][mask] = ans;\n    }\n    public int maxScore(List<List<Integer>> grid) {\n        int n = grid.size();\n        int m = grid.get(0).size();\n        dp = new int[101][1 << n];\n        for (int[] row : dp) Arrays.fill(row, -1);        \n        a = new ArrayList<>();\n        for (int i = 0; i <= 100; i++) {\n            a.add(new ArrayList<>());\n        }\n        int maxi = 0;\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < m; j++) {\n                a.get(grid.get(i).get(j)).add(i);\n                maxi = Math.max(maxi, grid.get(i).get(j));\n            }\n        }\n\n        return calc(maxi, 0);\n    }\n}\n```
['Java']