You are given a square board&nbsp;of characters. You can move on the board starting at the bottom right square marked with the character&nbsp;'S'.

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

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, taken modulo 10^9 + 7.

In case there is no path, return&nbsp;[0, 0].

&nbsp;
Example 1:
Input: board = ["E23","2X2","12S"]
Output: [7,1]
Example 2:
Input: board = ["E12","1X1","21S"]
Output: [4,2]
Example 3:
Input: board = ["E11","XXX","11S"]
Output: [0,0]

&nbsp;
Constraints:


	2 &lt;= board.length == board[i].length &lt;= 100

class Solution:
    def pathsWithMaxScore(self, board: List[str]) -> List[int]:

        ## Basic information: height and width of board
        h, w = len(board), len(board[0])

        # ----------------------------------------------------------------------------

        # Use pathon native cahce as memoization for DP
        @cache
        def visit(i, j):

            if i == h-1 and j == w-1:
                ## Base case:

                # score for start coordinate = 0
                # paht count for start coordinate = 1
                return 0, 1

            elif i >= w or j >= h or board[i][j] == 'X':

                ## Base case:
                # Out-of-boundary, or meet obstacle
                return 0, 0

            ## General case:
            # update from three possible preivous moves from right, down, and diagonal

            right_score, right_path_count = visit(i, j+1)
            down_score, down_path_count = visit(i+1, j)
            diag_score, diag_path_count = visit(i+1, j+1)

            max_prevScore = max(right_score, down_score, diag_score)

            cur_path_count = 0
            cur_score = int(board[i][j]) if board[i][j] != "E" else 0

            if right_score == max_prevScore : cur_path_count += right_path_count
            if down_score == max_prevScore : cur_path_count += down_path_count
            if diag_score == max_prevScore : cur_path_count += diag_path_count

            return max_prevScore + cur_score, cur_path_count
        # ----------------------------------------------------------------------------

        ## Remark: Remember to take modulo by constant, this is defined by description
        CONST = 10**9 + 7
        maxScore, validPathCount = visit(0, 0)

        return [maxScore % CONST, validPathCount % CONST] if validPathCount else [0, 0]
class Solution {
    public int[] pathsWithMaxScore(List<String> board) {
        int M = (int)1e9+7;
        int m = board.size();
        int n = board.get(0).length();
        int[][] dp = new int[m][n];
        int[][] ways = new int[m][n];
        ways[0][0]=1; // base case.
        int[][] dirs = {{-1, 0}, {0, -1}, {-1, -1}}; // all 3 ways where we can travel.
        for (int i=0;i<m;i++){
            for (int j=0;j<n;j++){
                if (board.get(i).charAt(j)=='X'){
                    continue;
                }
                int cur = Character.isDigit(board.get(i).charAt(j))?board.get(i).charAt(j)-'0':0;
                for (int[] d : dirs){
                    int x = d[0]+i;
                    int y = d[1]+j;
                    if (x < 0 || y < 0 || ways[x][y] == 0){
                        continue;
                    }
                    if (cur+dp[x][y]>dp[i][j]){
                        dp[i][j]=cur+dp[x][y];
                        ways[i][j]=0;
                    }
                    if (cur+dp[x][y]==dp[i][j]){
                        ways[i][j]+=ways[x][y];
                        ways[i][j]%=M;
                    }
                }
            }
        }
        return new int[]{dp[m-1][n-1], ways[m-1][n-1]};
    }
}
class Solution {
public:
    int mod=1e9+7;
    map<pair<int,int>,pair<int,int>>h;
    pair<int,int>solve(vector<string>&board,int i,int j,int n,int m)
    {
        //base case if you reach top left then 1 path hence return 1
        if(i==0 && j==0)return {0,1};
        //return 0 as no path is detected
        if(i<0 || j<0 || i>=n || j>=m || board[i][j]=='X')return {INT_MIN,0};
        //check if it is stored or not
        if(h.find({i,j})!=h.end())return h[{i,j}];
        int no=0,cnt=0;
        if(board[i][j]!='S')no=board[i][j]-'0';
        //top ,left ,top left
        auto a=solve(board,i-1,j,n,m);
        auto b=solve(board,i,j-1,n,m);
        auto c=solve(board,i-1,j-1,n,m);
        //maxi ans
        int curr=(max(a.first,max(b.first,c.first)))%mod;
        //if maxi ans == a , b , c then increament count of a ,b,c
        if(curr==a.first)cnt+=a.second;
        if(curr==b.first)cnt+=b.second;
        if(curr==c.first)cnt+=c.second;
        return h[{i,j}]={(curr+no)%mod,cnt%mod};
    }
    vector<int> pathsWithMaxScore(vector<string>& board) {
        auto ans=solve(board,board.size()-1,board[0].size()-1,board.size(),board[0].size());
        if(ans.first<0)return {0,0};
        return {ans.first%mod,ans.second%mod};
    }
};
var pathsWithMaxScore = function(board) {
  let n = board.length, dp = Array(n + 1).fill(0).map(() => Array(n + 1).fill(0).map(() => [-Infinity, 0]));
  let mod = 10 ** 9 + 7;
  dp[n - 1][n - 1] = [0, 1]; // [max score, number of paths]

  for (let i = n - 1; i >= 0; i--) {
    for (let j = n - 1; j >= 0; j--) {
      if (board[i][j] === 'X' || board[i][j] === 'S') continue;

      let paths = [dp[i][j + 1], dp[i + 1][j + 1], dp[i + 1][j]];
      for (let [maxScore, numPaths] of paths) {
        if (dp[i][j][0] < maxScore) {
          dp[i][j] = [maxScore, numPaths];
        } else if (dp[i][j][0] === maxScore) {
          dp[i][j][1] = (dp[i][j][1] + numPaths) % mod;
        }
      }
      let score = board[i][j] === 'E' ? 0 : Number(board[i][j]);
      dp[i][j][0] += score;
    }
  }
  return dp[0][0][1] === 0 ? [0, 0] : dp[0][0];
};
Number of Paths with Max Score