<p>You are given a square <code>board</code>&nbsp;of characters. You can move on the board starting at the bottom right square marked with the character&nbsp;<code>&#39;S&#39;</code>.</p>

<p>You need&nbsp;to reach the top left square marked with the character <code>&#39;E&#39;</code>. The rest of the squares are labeled either with a numeric character&nbsp;<code>1, 2, ..., 9</code> or with an obstacle <code>&#39;X&#39;</code>. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.</p>

<p>Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum, <strong>taken modulo <code>10^9 + 7</code></strong>.</p>

<p>In case there is no path, return&nbsp;<code>[0, 0]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> board = ["E23","2X2","12S"]
<strong>Output:</strong> [7,1]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> board = ["E12","1X1","21S"]
<strong>Output:</strong> [4,2]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> board = ["E11","XXX","11S"]
<strong>Output:</strong> [0,0]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= board.length == board[i].length &lt;= 100</code></li>
</ul>
from typing import List
X = int(1e9 + 7)  
class Solution:
    def __init__(self):
        self.grid = []
        self.size = 0
        self.scoreMatrix = []
        self.pathCountMatrix = []

    def pathsWithMaxScore(self, grid: List[str]) -> List[int]:
        self.grid = grid
        self.size = len(grid)
        self.scoreMatrix = [[-1] * self.size for _ in range(self.size)]
        self.pathCountMatrix = [[0] * self.size for _ in range(self.size)]

        # Starting point at the bottom-right has a score of 0 and 1 path.
        self.scoreMatrix[self.size - 1][self.size - 1] = 0
        self.pathCountMatrix[self.size - 1][self.size - 1] = 1

        # Calculate maximum score and paths count from bottom-right to top-left
        for row in range(self.size - 1, -1, -1):
            for col in range(self.size - 1, -1, -1):
                self._checkAndUpdate(row, col, row + 1, col)
                self._checkAndUpdate(row, col, row, col + 1)
                self._checkAndUpdate(row, col, row + 1, col + 1)
                if self.scoreMatrix[row][col] != -1:
                    cell = self.grid[row][col]
                    if '0' <= cell <= '9':
                        self.scoreMatrix[row][col] += int(cell)  # Update score for numeric cells

        # Prepare and return the results
        result = [0, 0]
        if self.scoreMatrix[0][0] != -1:
            result[0] = self.scoreMatrix[0][0]  # Maximum score
            result[1] = self.pathCountMatrix[0][0]  # Number of paths with the maximum score
        return result

    def _checkAndUpdate(self, currentRow: int, currentCol: int, prevRow: int, prevCol: int):
        # Skip update if out of bounds, previous cell was unreachable, or current cell is blocked or the start
        if prevRow >= self.size or prevCol >= self.size \
           or self.scoreMatrix[prevRow][prevCol] == -1 \
           or self.grid[currentRow][currentCol] == 'X' \
           or self.grid[currentRow][currentCol] == 'S':
            return
        
        # Update if a higher score is found
        if self.scoreMatrix[prevRow][prevCol] > self.scoreMatrix[currentRow][currentCol]:
            self.scoreMatrix[currentRow][currentCol] = self.scoreMatrix[prevRow][prevCol]
            self.pathCountMatrix[currentRow][currentCol] = self.pathCountMatrix[prevRow][prevCol]
        # If the current score is equal, add the paths count
        elif self.scoreMatrix[prevRow][prevCol] == self.scoreMatrix[currentRow][currentCol]:
            self.pathCountMatrix[currentRow][currentCol] = (self.pathCountMatrix[currentRow][currentCol] + self.pathCountMatrix[prevRow][prevCol]) % X
     
17500