Given a gold mine called M of (n x m) dimensions. Each field in this mine contains a positive integer which is the amount of gold in tons. Initially the miner can start from any row in the first column. From a given cell, the miner can move 
	to the cell diagonally up towards the right 
	to the right
	to the cell diagonally down towards the right
Find out maximum amount of gold which he can collect.
Example 1:
Input: n = 3, m = 3
M = {{1, 3, 3},
     {2, 1, 4},
     {0, 6, 4}};
Output: 12
Explaination: 
The path is {(1,0) -> (2,1) -> (2,2)}.
Example 2:
Input: n = 4, m = 4
M = {{1, 3, 1, 5},
     {2, 2, 4, 1},
     {5, 0, 2, 3},
     {0, 6, 1, 2}};
Output: 16
Explaination: 
The path is {(2,0) -> (3,1) -> (2,2) 
-> (2,3)} or {(2,0) -> (1,1) -> (1,2) 
-> (0,3)}.
Your Task:
You do not need to read input or print anything. Your task is to complete the function maxGold() which takes the values n, m and the mine M as input parameters and returns the maximum amount of gold that can be collected.
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n, m ≤ 50
0 ≤ M[i][j] ≤ 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Dynamic programming

Starter code: # User function Template for Python3



class Solution:

    def maxGold(self, n, m, M):

        # code here

Solutions:
class Solution:

	def maxGold(self, n, m, M):
		dp = [[0 for j in range(m)] for i in range(n)]
		for i in range(n):
			dp[i][0] = M[i][0]
		for j in range(1, m):
			for i in range(n):
				leftUp = float('-inf')
				leftDown = float('-inf')
				if i > 0:
					leftUp = dp[i - 1][j - 1]
				if i < n - 1:
					leftDown = dp[i + 1][j - 1]
				left = dp[i][j - 1]
				dp[i][j] = max(left, leftUp, leftDown) + M[i][j]
		maxi = float('-inf')
		for i in range(n):
			maxi = max(maxi, dp[i][m - 1])
		return maxi


class Solution:

	def fin(self, i, j, n, m, mat, dp):
		if j == m:
			return 0
		if i < 0 or i == n:
			return 0
		if dp[i][j] != -1:
			return dp[i][j]
		else:
			right = mat[i][j] + self.fin(i, j + 1, n, m, mat, dp)
			digup = mat[i][j] + self.fin(i - 1, j + 1, n, m, mat, dp)
			digdown = mat[i][j] + self.fin(i + 1, j + 1, n, m, mat, dp)
			dp[i][j] = max(right, max(digup, digdown))
			return dp[i][j]

	def maxGold(self, n, m, M):
		maxi = 0
		j = 0
		dp = [[-1 for i in range(m + 1)] for j in range(n + 1)]
		for i in range(n):
			ans = self.fin(i, j, n, m, M, dp)
			maxi = max(maxi, ans)
		return maxi


class Solution:

	def maxGold(self, n, m, M):
		dp = {}

		def fun(i, j):
			if i == n or j == m or i < 0 or (j < 0):
				return 0
			if (i, j) in dp:
				return dp[i, j]
			dp[i, j] = M[i][j] + max(fun(i + 1, j + 1), fun(i, j + 1), fun(i - 1, j + 1))
			return dp[i, j]
		ans = 0
		for i in range(n):
			ans = max(ans, fun(i, 0))
		return ans


import sys
import copy

class Solution:

	def rec_sol(self, n, m, M, i, j):
		if j < 0:
			return 0
		maxa = M[i][j] + self.rec_sol(n, m, M, i, j - 1)
		if i != 0:
			maxa = max(maxa, M[i][j] + self.rec_sol(n, m, M, i - 1, j - 1))
		if i != n - 1:
			maxa = max(maxa, M[i][j] + self.rec_sol(n, m, M, i + 1, j - 1))

	def dp_sol(self, n, m, M):
		dp = []
		for k in range(n):
			dp.append(M[k][0])
		dp_n = [-1] * n
		for j in range(1, m):
			for i in range(0, n):
				maxa = M[i][j] + dp[i]
				if i != 0:
					maxa = max(maxa, M[i][j] + dp[i - 1])
				if i != n - 1:
					maxa = max(maxa, M[i][j] + dp[i + 1])
				dp_n[i] = maxa
			dp = copy.deepcopy(dp_n)
			dp_n = [-1] * n
		return max(dp)

	def maxGold(self, n, m, M):
		return self.dp_sol(n, m, M)


class Solution:

	def maxGold(self, n, m, M):
		dp = [[0 for j in range(m)] for i in range(n)]
		for i in range(n):
			dp[i][m - 1] = M[i][m - 1]
		for j in range(m - 2, -1, -1):
			for i in range(n):
				dp[i][j] = M[i][j] + dp[i][j + 1]
				if i > 0:
					dp[i][j] = max(dp[i][j], M[i][j] + dp[i - 1][j + 1])
				if i < n - 1:
					dp[i][j] = max(dp[i][j], M[i][j] + dp[i + 1][j + 1])
		maxGold = 0
		for i in range(n):
			maxGold = max(maxGold, dp[i][0])
		return maxGold


class Solution:

	def maxGold(self, n, m, M):
		dp = [[0 for i in range(m)] for j in range(n)]
		for j in reversed(range(m)):
			for i in range(n):
				if j == m - 1:
					dp[i][j] = M[i][j]
				elif n == 1:
					dp[i][j] = M[i][j] + dp[i][j + 1]
				elif i == 0:
					dp[i][j] = M[i][j] + max(dp[i][j + 1], dp[i + 1][j + 1])
				elif i == n - 1:
					dp[i][j] = M[i][j] + max(dp[i - 1][j + 1], dp[i][j + 1])
				else:
					dp[i][j] = M[i][j] + max(dp[i - 1][j + 1], max(dp[i][j + 1], dp[i + 1][j + 1]))
		maxi = dp[0][0]
		for i in range(1, n):
			if maxi < dp[i][0]:
				maxi = dp[i][0]
		return maxi


import sys

class Solution:

	def maxGold(self, n, m, M):

		def gold(i, j, n, m, M, dp):
			if i >= n or j >= m or i < 0:
				return 0
			if dp[i][j] != -1:
				return dp[i][j]
			rd = gold(i - 1, j + 1, n, m, M, dp)
			r = gold(i, j + 1, n, m, M, dp)
			ld = gold(i + 1, j + 1, n, m, M, dp)
			dp[i][j] = M[i][j] + max(rd, r, ld)
			return dp[i][j]
		maxi = -sys.maxsize
		dp = [[-1 for i in range(m)] for j in range(n)]
		for i in range(n):
			maxi = max(maxi, gold(i, 0, n, m, M, dp))
		return maxi


class Solution:

	def maxGold(self, n, m, M):
		stateMap = [[0 for i in range(m)] for _ in range(n)]
		for i in range(n):
			stateMap[i][0] = M[i][0]
		for c in range(1, m):
			for r in range(n):
				stateMap[r][c] = stateMap[r][c - 1]
				if r != 0:
					stateMap[r][c] = max(stateMap[r][c], stateMap[r - 1][c - 1])
				if r != n - 1:
					stateMap[r][c] = max(stateMap[r][c], stateMap[r + 1][c - 1])
				stateMap[r][c] += M[r][c]
		maxVal = 0
		for i in range(n):
			maxVal = max(maxVal, stateMap[i][m - 1])
		return maxVal


class Solution:

	def maxGold(self, n, m, M):
		d = {}

		def gold(i, j):
			key = (i, j)
			if i == n or j == m or i < 0 or (j < 0):
				return 0
			if key in d:
				return d[key]
			d[key] = M[i][j] + max(gold(i, j + 1), gold(i + 1, j + 1), gold(i - 1, j + 1))
			return d[key]
		ans = 0
		for i in range(n):
			ans = max(ans, gold(i, 0))
		return ans


class Solution:

	def maxGold(self, n, m, M):
		dp = [[0 for _ in range(m)] for _ in range(n)]
		dir = [(-1, -1), (0, -1), (1, -1)]
		ans = 0
		for j in range(m):
			for i in range(n):
				dp[i][j] = M[i][j]
				for d in dir:
					x = i + d[0]
					y = j + d[1]
					if x >= 0 and x < n and (y >= 0) and (y < m):
						dp[i][j] = max(dp[i][j], M[i][j] + dp[x][y])
				ans = max(ans, dp[i][j])
		return ans


class Solution:

	def maxGold(self, n, m, M):
		dp = [[0 for _ in range(m)] for _ in range(n)]
		for col in range(m - 1, -1, -1):
			for row in range(n - 1, -1, -1):
				righttop = 0
				if col != m - 1 and row != 0:
					righttop = dp[row - 1][col + 1]
				right = 0
				if col != m - 1:
					right = dp[row][col + 1]
				rightbottom = 0
				if col != m - 1 and row != n - 1:
					rightbottom = dp[row + 1][col + 1]
				dp[row][col] = M[row][col] + max(right, righttop, rightbottom)
		mini = float('-inf')
		for i in range(n):
			mini = max(mini, dp[i][0])
		return mini


def maxGoldCollected(mine, n, m):
	dp = [[0] * m for i in range(n)]
	for i in range(n):
		dp[i][m - 1] = mine[i][m - 1]
	for j in range(m - 2, -1, -1):
		for i in range(n):
			(upDiagonal, downDiagonal, right) = (0, 0, 0)
			if i > 0:
				upDiagonal = dp[i - 1][j + 1]
			if i < n - 1:
				downDiagonal = dp[i + 1][j + 1]
			right = dp[i][j + 1]
			dp[i][j] = mine[i][j] + max(right, upDiagonal, downDiagonal)
	ans = 0
	for i in range(n):
		ans = max(ans, dp[i][0])
	return ans

class Solution:

	def maxGold(self, n, m, M):
		return maxGoldCollected(M, n, m)


class Solution:

	def maxGold(self, n, m, mat):
		dp = [[-1] * m for i in range(n)]

		def findsol(i, j):
			if i < 0 or j < 0 or i >= n or (j >= m):
				return 0
			if dp[i][j] != -1:
				return dp[i][j]
			upd = mat[i][j] + findsol(i - 1, j + 1)
			r = mat[i][j] + findsol(i, j + 1)
			ld = mat[i][j] + findsol(i + 1, j + 1)
			dp[i][j] = max(upd, r, ld)
			return max(upd, r, ld)
		maxval = 0
		for i in range(n):
			maxval = max(maxval, findsol(i, 0))
		return maxval


class Solution:

	def maxGold(self, n, m, M):
		f = [i[0] for i in M]
		s = [0 for i in range(len(M))]
		if m == 1:
			return max(f)
		elif n == 1:
			return sum([i for i in M[0]])
		for i in range(1, m):
			s[0] = M[0][i] + max(f[0], f[1])
			for x in range(1, n - 1):
				s[x] = M[x][i] + max(f[x], f[x + 1], f[x - 1])
			s[-1] = M[-1][i] + max(f[-1], f[-2])
			f = [k for k in s]
		return max(s)


class Solution:

	def maxGold(self, n, m, M):

		def calc(i, j, grid, l):
			(m, n) = (len(grid[0]), len(grid))
			if (i, j) in l:
				return l[i, j]
			elif j == m or i == n or i < 0:
				return 0
			else:
				l[i, j] = grid[i][j] + max(calc(i, j + 1, grid, l), calc(i + 1, j + 1, grid, l), calc(i - 1, j + 1, grid, l))
				return l[i, j]
		ma = 0
		l = {}
		for i in range(len(M)):
			ma = max(ma, calc(i, 0, M, l))
		return ma


class Solution:

	def maxGold(self, m, n, M):

		def ans(M, i, j, m, n, dp):
			if i < 0 or i >= m:
				return -10000000000.0
			if j == 0:
				return M[i][0]
			if dp[i][j] != -1:
				return dp[i][j]
			else:
				left_up = M[i][j] + ans(M, i - 1, j - 1, m, n, dp)
				left = M[i][j] + ans(M, i, j - 1, m, n, dp)
				left_down = M[i][j] + ans(M, i + 1, j - 1, m, n, dp)
				dp[i][j] = max(left_up, left, left_down)
				return dp[i][j]
		maxi = -10000000000.0
		dp = [[-1 for _ in range(n)] for _ in range(m)]
		for i in range(m):
			maxi = max(maxi, ans(M, i, n - 1, m, n, dp))
		return maxi


class Solution:

	def collectGold(self, gold, x, y, n, m, dp):
		if x < 0 or x == n or y == m:
			return 0
		if dp[x][y] != -1:
			return dp[x][y]
		rightUpperDiagonal = self.collectGold(gold, x - 1, y + 1, n, m, dp)
		right = self.collectGold(gold, x, y + 1, n, m, dp)
		rightLowerDiagonal = self.collectGold(gold, x + 1, y + 1, n, m, dp)
		dp[x][y] = gold[x][y] + max(max(rightUpperDiagonal, rightLowerDiagonal), right)
		return dp[x][y]

	def maxGold(self, n, m, gold):
		mg = 0
		dp = [[-1 for i in range(m)] for j in range(n)]
		for i in range(n):
			goldCollected = self.collectGold(gold, i, 0, n, m, dp)
			mg = max(mg, goldCollected)
		return mg


class Solution:

	def maxGold(self, n, m, M):
		r_dir = [-1, 0, 1]
		c_dir = [-1, -1, -1]
		ans = 0
		for j in range(m):
			for i in range(n):
				gold = 0
				for k in range(3):
					r = i + r_dir[k]
					c = j + c_dir[k]
					if r >= 0 and c >= 0 and (r < n) and (c < m):
						gold = max(gold, M[r][c])
				M[i][j] += gold
				ans = max(ans, M[i][j])
		return ans


class Solution:

	def maxGold(self, n, m, M):
		dp = [[0 for i in range(m)] for j in range(n)]
		for col in range(m - 1, -1, -1):
			for row in range(n):
				right = 0
				if col != m - 1:
					right = dp[row][col + 1]
				righttop = 0
				if col != m - 1 and row != 0:
					righttop = dp[row - 1][col + 1]
				rightdown = 0
				if col != m - 1 and row != n - 1:
					rightdown = dp[row + 1][col + 1]
				dp[row][col] = M[row][col] + max(right, righttop, rightdown)
		maxi = float('-inf')
		for i in range(n):
			maxi = max(maxi, dp[i][0])
		return maxi


class Solution:

	def goldmine(self, x, y, rows, cols, grid, dp):
		for i in range(cols - 1, -1, -1):
			for j in range(rows - 1, -1, -1):
				if j == rows - 1 and i == cols - 1:
					dp[j][i] = grid[j][i]
				elif j == rows - 1:
					dp[j][i] = max(dp[j][i + 1], dp[j - 1][i + 1]) + grid[j][i]
				elif i == cols - 1:
					dp[j][i] = grid[j][i]
				elif j == 0:
					dp[j][i] = max(dp[j][i + 1], dp[j + 1][i + 1]) + grid[j][i]
				else:
					dp[j][i] = max(dp[j][i + 1], dp[j - 1][i + 1], dp[j + 1][i + 1]) + grid[j][i]
		return max(list(zip(*dp))[0])

	def maxGold(self, n, m, M):
		dp = [[0 for _ in range(m)] for _ in range(n)]
		return self.goldmine(i, 0, n, m, M, dp)


class Solution:

	def goldmine(self, x, y, rows, cols, grid, dp):
		if y == cols - 1:
			return grid[x][y]
		if dp[x][y]:
			return dp[x][y]
		max_gold = float('-inf')
		for (i, j) in zip([0, -1, 1], [1, 1, 1]):
			if -1 < x + i < rows and -1 < y + j < cols:
				max_gold = max(max_gold, self.goldmine(x + i, y + j, rows, cols, grid, dp))
		max_gold += grid[x][y]
		dp[x][y] = max_gold
		return max_gold

	def maxGold(self, n, m, M):
		dp = [[0 for _ in range(m)] for _ in range(n)]
		max_val = float('-inf')
		for i in range(n):
			max_val = max(max_val, self.goldmine(i, 0, n, m, M, dp))
		return max_val


class Solution:

	def maxGold(self, n, m, M):
		if n == 1:
			return sum(M[0])
		for j in range(m - 1, -1, -1):
			for i in range(n):
				if j == m - 1:
					pass
				elif i == 0:
					M[i][j] += max(M[i][j + 1], M[i + 1][j + 1])
				elif i == n - 1:
					M[i][j] += max(M[i][j + 1], M[i - 1][j + 1])
				else:
					M[i][j] += max(M[i][j + 1], M[i + 1][j + 1], M[i - 1][j + 1])
		ans = 0
		for i in range(n):
			ans = max(ans, M[i][0])
		return ans


class Solution:

	def maxGold(self, n, m, M):
		if n == 1:
			ans = 0
			for i in range(m):
				ans += M[0][i]
			return ans
		for i in range(1, m):
			for j in range(n):
				if j == 0:
					M[0][i] += max(M[0][i - 1], M[1][i - 1])
				elif j == n - 1:
					M[n - 1][i] += max(M[n - 1][i - 1], M[n - 2][i - 1])
				else:
					M[j][i] += max(M[j][i - 1], M[j - 1][i - 1], M[j + 1][i - 1])
		ma = M[0][m - 1]
		for i in range(n):
			ma = max(ma, M[i][m - 1])
		return ma


class Solution:

	def maxGold(self, n, m, M):
		dp = {}

		def isValid(X, Y):
			if X >= 0 and X < n and (Y >= 0) and (Y < m):
				return True
			return False

		def recursion(i, j):
			if j == m - 1:
				return M[i][j]
			elif (i, j) in dp:
				return dp[i, j]
			else:
				profit = 0
				dx = [-1, 0, 1]
				dy = [1, 1, 1]
				for (x, y) in zip(dx, dy):
					if isValid(i + x, y + j):
						possible = M[i][j] + recursion(i + x, j + y)
						profit = max(possible, profit)
				dp[i, j] = profit
				return profit
		maxpr = float('-inf')
		for i in range(n):
			maxpr = max(maxpr, recursion(i, 0))
		return maxpr


class Solution:

	def maxGold(self, n, m, M):
		if n == 1:
			return sum(M[0])
		for c in range(1, m):
			for r in range(n):
				if r == 0:
					M[r][c] += max(M[r][c - 1], M[r + 1][c - 1])
				elif r == n - 1:
					M[r][c] += max(M[r][c - 1], M[r - 1][c - 1])
				else:
					M[r][c] += max(M[r - 1][c - 1], M[r][c - 1], M[r + 1][c - 1])
		res = 0
		for r in range(n):
			res = max(res, M[r][-1])
		return res


class Solution:

	def maxGold(self, m, n, mat):
		for j in range(1, n):
			for i in range(m):
				dup = ddown = 0
				left = mat[i][j - 1]
				if i > 0:
					dup = mat[i - 1][j - 1]
				if i < m - 1:
					ddown = mat[i + 1][j - 1]
				mat[i][j] += max(dup, ddown, left)
		return max((mat[i][-1] for i in range(m)))


class Solution:

	def maxGold(self, n, m, M):
		dp = [[None] * (m + 1) for _ in range(n + 2)]
		for i in range(n + 2):
			dp[i][0] = 0
		for j in range(m + 1):
			dp[0][j] = 0
			dp[n + 1][j] = 0
		for j in range(1, m + 1):
			for i in range(1, n + 1):
				dp[i][j] = M[i - 1][j - 1] + max(dp[i][j - 1], dp[i + 1][j - 1], dp[i - 1][j - 1])
		res = -1
		for i in range(n + 1):
			res = max(res, dp[i][m])
		return res


class Solution:

	def maxGold(self, n, m, M):
		res = 0
		dp = [[None] * m for _ in range(n)]

		def fun(i, j):
			if i < 0 or i >= n:
				return 0
			if j == m - 1:
				return M[i][j]
			if dp[i][j] != None:
				return dp[i][j]
			dp[i][j] = M[i][j] + max(fun(i - 1, j + 1), fun(i, j + 1), fun(i + 1, j + 1))
			return dp[i][j]
		res = -1
		for i in range(n):
			res = max(res, fun(i, 0))
		return res


from sys import maxsize

class Solution:

	def maxGold(self, n, m, mine):

		def goldMineProblem(row, col, mine, n, m, memo=dict()):
			if (row, col) in memo:
				return memo[row, col]
			dirs = [-1, 0, 1]
			ans = 0
			for j in dirs:
				if row + j < 0 or row + j >= n or col + 1 >= m:
					continue
				temp = goldMineProblem(row + j, col + 1, mine, n, m, memo)
				ans = max(temp, ans)
			ans += mine[row][col]
			memo[row, col] = ans
			return memo[row, col]
		totalAns = -maxsize
		for i in range(n):
			totalAns = max(totalAns, goldMineProblem(i, 0, mine, n, m))
		return totalAns


class Solution:

	def maxGold(self, n, m, mat):
		if n == 1:
			return sum(mat[0])
		for j in range(1, m):
			for i in range(n):
				if i == 0:
					mat[i][j] += max(mat[i][j - 1], mat[i + 1][j - 1])
				elif i == n - 1:
					mat[i][j] += max(mat[i][j - 1], mat[i - 1][j - 1])
				else:
					mat[i][j] += max(mat[i][j - 1], mat[i - 1][j - 1], mat[i + 1][j - 1])
		ans = float('-inf')
		for i in range(n):
			ans = max(ans, mat[i][m - 1])
		return ans


class Solution:

	def maxGold(self, n, m, M):
		if n == 1:
			a = 0
			for i in range(0, m):
				a = a + M[0][i]
			return a
		dp = [[0 for i in range(0, m)] for j in range(0, n)]
		for i in range(0, n):
			dp[i][m - 1] = M[i][m - 1]
		for j in range(m - 2, -1, -1):
			for i in range(0, n):
				if i == 0:
					dp[i][j] = M[i][j] + max(dp[i][j + 1], dp[i + 1][j + 1])
				elif i == n - 1:
					dp[i][j] = M[i][j] + max(dp[i - 1][j + 1], dp[i][j + 1])
				else:
					dp[i][j] = M[i][j] + max(dp[i][j + 1], dp[i - 1][j + 1], dp[i + 1][j + 1])
		m1 = 0
		for i in range(0, n):
			m1 = max(dp[i][0], m1)
		return m1


class Solution:

	def maxGold(self, n, m, M):
		mGold = 0
		dp = [[-1 for i in range(m)] for j in range(n)]
		for i in range(n):
			cGold = self.collectGold(M, i, 0, n, m, dp)
			mGold = max(mGold, cGold)
		return mGold

	def collectGold(self, gold, x, y, n, m, dp):
		if x < 0 or x == n or y == m:
			return 0
		if dp[x][y] != -1:
			return dp[x][y]
		left = self.collectGold(gold, x - 1, y + 1, n, m, dp)
		line = self.collectGold(gold, x, y + 1, n, m, dp)
		right = self.collectGold(gold, x + 1, y + 1, n, m, dp)
		dp[x][y] = gold[x][y] + max(max(left, line), right)
		return dp[x][y]


import numpy as np

class Solution:

	def maxGold(self, n, m, M):
		y = 0
		if n == 1:
			for i in range(m):
				y += M[0][i]
			return y
		dp = [[-1] * m for i in range(n)]
		for i in range(n):
			dp[i][0] = M[i][0]
		for j in range(1, m):
			for i in range(0, n):
				if i == 0:
					dp[i][j] = max(dp[i][j - 1], dp[i + 1][j - 1]) + M[i][j]
				elif i == n - 1:
					dp[i][j] = max(dp[i - 1][j - 1], dp[i][j - 1]) + M[i][j]
				else:
					dp[i][j] = max(dp[i - 1][j - 1], dp[i][j - 1], dp[i + 1][j - 1]) + M[i][j]
		maxx = 0
		for i in range(0, n):
			if maxx < dp[i][m - 1]:
				maxx = dp[i][m - 1]
		return maxx


class Solution:

	def maxGold(self, m, n, M):
		Max = 100
		result = 0
		dp = [[0 for i in range(n)] for j in range(m)]
		for i in range(n - 1, -1, -1):
			for j in range(m):
				if i == n - 1:
					right = 0
				else:
					right = dp[j][i + 1]
				if j == 0 or i == n - 1:
					r_up = 0
				else:
					r_up = dp[j - 1][i + 1]
				if j == m - 1 or i == n - 1:
					r_down = 0
				else:
					r_down = dp[j + 1][i + 1]
				dp[j][i] = M[j][i] + max(right, r_up, r_down)
		result = dp[0][0]
		for i in range(1, m):
			result = max(result, dp[i][0])
		return result


class Solution:

	def maxGold(self, n, m, M):
		if n == 1:
			return sum(M[0])
		else:
			for j in range(1, m):
				for i in range(n):
					if i == 0:
						M[i][j] = M[i][j] + max(M[i][j - 1], M[i + 1][j - 1])
					elif i == n - 1:
						M[i][j] = M[i][j] + max(M[i - 1][j - 1], M[i][j - 1])
					else:
						M[i][j] = M[i][j] + max(M[i - 1][j - 1], M[i][j - 1], M[i + 1][j - 1])
		l = []
		for i in M:
			l.append(i[-1])
		return max(l)


class Solution:

	def maxGold(self, n, m, M):
		if n == 1:
			return sum(M[0])
		for j in range(m - 2, -1, -1):
			for i in range(0, n):
				if i == 0:
					M[i][j] = M[i][j] + max(M[i][j + 1], M[i + 1][j + 1])
				elif i == n - 1:
					M[i][j] = M[i][j] + max(M[i - 1][j + 1], M[i][j + 1])
				else:
					M[i][j] = M[i][j] + max(M[i - 1][j + 1], M[i][j + 1], M[i + 1][j + 1])
		x = []
		for i in M:
			x.append(i[0])
		res = max(x)
		return res


class Solution:

	def maxGold(self, n, m, M):
		s = 0
		if n == 1:
			for i in M[0]:
				s += i
			return s
		dp = [[0 for i in range(m)] for j in range(n)]
		for col in range(m - 1, -1, -1):
			for row in range(n):
				if col == m - 1:
					dp[row][col] = M[row][col]
				elif row == 0:
					dp[row][col] = M[row][col] + max(dp[row][col + 1], dp[row + 1][col + 1])
				elif row == n - 1:
					dp[row][col] = M[row][col] + max(dp[row][col + 1], dp[row - 1][col + 1])
				else:
					dp[row][col] = M[row][col] + max(dp[row][col + 1], max(dp[row - 1][col + 1], dp[row + 1][col + 1]))
		result = dp[0][0]
		for i in range(n):
			result = max(result, dp[i][0])
		return result


class Solution:

	def maxGold(self, n, m, M):
		if n == 1:
			res = 0
			for i in range(0, m):
				res += M[0][i]
			return res
		for j in range(1, m):
			for i in range(0, n):
				if i == 0:
					M[i][j] += max(M[i][j - 1], M[i + 1][j - 1])
				elif i == n - 1:
					M[i][j] += max(M[i][j - 1], M[i - 1][j - 1])
				else:
					M[i][j] += max(M[i][j - 1], M[i - 1][j - 1], M[i + 1][j - 1])
		res = 0
		for i in range(n):
			res = max(res, M[i][m - 1])
		return res


class Solution:

	def maxGold(self, n, m, M):

		def recurion(r, c):
			if dp[r][c] != -1:
				return dp[r][c]
			if r >= n or c >= m or r < 0 or (c < 0):
				return 0
			dp[r][c] = M[r][c] + max(recurion(r - 1, c + 1), recurion(r, c + 1), recurion(r + 1, c + 1))
			return dp[r][c]
		dp = [[-1 for i in range(m + 1)] for i in range(n + 1)]
		maxi = -999999
		for row in range(n):
			maxi = max(maxi, recurion(row, 0))
		return maxi


class Solution:

	def max(a, b, c):
		if a > b and a > b:
			return a
		elif b > c and b > a:
			return b
		return c

	def maxGold(self, n, m, M):
		res = M
		i = m - 2
		while i != -1:
			j = 0
			while j != n:
				a = 0
				b = 0
				if j != 0:
					a = res[j - 1][i + 1]
				if j != n - 1:
					b = res[j + 1][i + 1]
				res[j][i] = max(a, res[j][i + 1], b) + res[j][i]
				j += 1
			i -= 1
		ans = 0
		for i in range(n):
			if ans < res[i][0]:
				ans = res[i][0]
		return ans


class Solution:

	def maxGold(self, n, m, M):
		dp = []
		for j in range(m - 2, -1, -1):
			for i in range(n):
				rightup = 0
				right = 0
				rightdown = 0
				if i - 1 >= 0:
					rightup = M[i - 1][j + 1]
				if i + 1 < n:
					rightdown = M[i + 1][j + 1]
				right = M[i][j + 1]
				M[i][j] += max(right, rightup, rightdown)
		res = 0
		for i in range(len(M)):
			res = max(res, max(M[i]))
		return res


class Solution:

	def maxGold(self, n, m, M):
		arr = M
		dp = [[0 for i in range(m)] for j in range(n)]
		maxi = float('-inf')
		for j in range(m - 1, -1, -1):
			for i in range(n - 1, -1, -1):
				if j == m - 1:
					dp[i][j] = arr[i][j]
				elif i == 0:
					if n == 1:
						dp[i][j] = arr[i][j] + dp[i][j + 1]
					else:
						dp[i][j] = arr[i][j] + max(dp[i][j + 1], dp[i + 1][j + 1])
				elif i == n - 1:
					dp[i][j] = arr[i][j] + max(dp[i][j + 1], dp[i - 1][j + 1])
				else:
					dp[i][j] = arr[i][j] + max(dp[i][j + 1], dp[i + 1][j + 1], dp[i - 1][j + 1])
				maxi = max(maxi, dp[i][j])
		return maxi


class Solution:

	def maxGold(self, n, m, M):
		table = [[-1] * m for i in range(n)]

		def maxgold(i, j, n, m, M):
			if i >= n or j >= m or i < 0:
				return 0
			if table[i][j] != -1:
				return table[i][j]
			x = M[i][j] + maxgold(i - 1, j + 1, n, m, M)
			y = M[i][j] + maxgold(i, j + 1, n, m, M)
			z = M[i][j] + maxgold(i + 1, j + 1, n, m, M)
			table[i][j] = max(x, y, z)
			return table[i][j]
		maxi = -1000.0
		for i in range(n):
			ans = maxgold(i, 0, n, m, M)
			maxi = max(ans, maxi)
		return maxi