Go Solution
golang []\nfunc maxScore(grid [][]int) int {\n    n := len(grid)\n    m := len(grid[0])\n\n    graph := make(map[int][]int)\n    values := make([]int, 0)\n    s
```golang []\nfunc maxScore(grid [][]int) int {\n    n := len(grid)\n    m := len(grid[0])\n\n    graph := make(map[int][]int)\n    values := make([]int, 0)\n    seen := make(map[int]struct{})\n\n    for i := 0; i < n; i++ {\n        for j := 0; j < m; j++ {\n            val := grid[i][j]\n            graph[val] = append(graph[val], i)\n            if _, ok := seen[val]; !ok {\n                values = append(values, val)\n            }\n            seen[val] = struct{}{}\n        }\n    }\n    sort.Ints(values)\n    for i := 0; i < len(values) / 2; i++ {\n        j := len(values) - i - 1\n        values[i], values[j] = values[j], values[i]\n    }\n\n    type mkey struct {\n        idx, visited, score int\n    }\n    memo := make(map[mkey]int)\n\n    var dfs func(int, int, int) int\n    dfs = func(idx, visited, score int) int {\n        if idx == len(values) {\n            return score\n        }\n\n        key := mkey{ idx, visited, score }\n        if _, ok := memo[key]; !ok {\n            v := values[idx]\n            scores := make([]int, 0)\n            for _, u := range graph[v] {\n                mask := 1 << u\n                if (visited & mask) != 0 {\n                    subscore := dfs(idx + 1, visited - mask, v + score)\n                    scores = append(scores, subscore)\n                }\n            }\n            if len(scores) == 0 {\n                scores = append(scores, dfs(idx + 1, visited, score))\n            }\n            best := scores[0]\n            for _, s := range scores[1:] {\n                if s > best {\n                    best = s\n                }\n            }\n            memo[key] = best\n        }\n        return memo[key]\n    }\n\n    return dfs(0, (1 << n) - 1, 0)\n}\n```
['Go']