Python3 (using dynamic programming)
\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:        \n        @cache\n        def helper(idx, seen):\n            if idx == l: \n   
```\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:        \n        @cache\n        def helper(idx, seen):\n            if idx == l: \n                return 0 \n            \n            else: \n                ans = 0\n                \n                for i in range(idx, l):\n                    (value, ith) = arr[i]\n                    \n                    if (seen >> ith) & 1 == 0: \n                        ans = max(ans, value + helper(nextIndex[value], seen ^ (2**ith)))\n                        \n                return ans \n            \n        m, n = len(grid), len(grid[0])\n        \n        arr = []\n        \n        for i in range(m):\n            for j in range(n):\n                arr.append((grid[i][j], i))\n                \n        arr.sort(key = lambda item: -item[0])\n        \n        nextIndex = {}\n        \n        l = last = len(arr)\n        \n        for i in range(l - 1, -1, -1):\n            value = arr[i][0]\n            \n            if value not in nextIndex: \n                nextIndex[value] = last\n                \n            last = i      \n        \n        return helper(0, 0)\n```
[]