BACKTRACKING
Intuition\n Describe your first thoughts on how to solve this problem. \nFind the maximum value. If the maximum appears in multiple rows, iterate these rows and
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFind the maximum value. If the maximum appears in multiple rows, iterate these rows and use backtracking to find the answer for remaining rows.\n\n# Code\n```python3 []\nclass Solution:\n    def maxScore(self, grid: List[List[int]]) -> int:\n               \n        def backtrack(grd, exceptions):\n            m = len(grd)\n            if m == 1:\n                temp = [itm for itm in grd[0] if itm not in exceptions]\n                if temp:\n                    return max(temp)\n                return 0\n            n = len(grd[0])\n            d = set() # set of rows that contain maximum outside exceptions\n            v = 0\n            for i in range(m):\n                for j in range(n):\n                    if exceptions and grd[i][j] in exceptions:\n                        continue\n                    if grd[i][j] > v:\n                        d = set()\n                        d.add(i)\n                        v = grd[i][j]\n                    elif grd[i][j] == v:\n                        d.add(i) \n            # Now v is the maximum. If v==0, then all numbers are\n            # in exceptions so we return 0.\n            if not v: \n                return 0\n            res = 0\n            for r in list(d):\n                tempset = exceptions\n                tempset.add(v)\n                temp = backtrack(grd[:r]+grd[(r+1):], tempset)+v\n                tempset.remove(v)\n                res = max(res,temp)\n            return res\n        \n        return backtrack(grid, set())\n                \n            \n        \n```
['Python3']