---
# System prepended metadata

title: 2/22 l924. P.7 搜集寶藏

---

https://zerojudge.tw/ShowProblem?problemid=l924
往終點只有往右跟往下 所以找左跟上的最優解
```pyython=
n, m = map(int, input().split())
z = [list(map(int, input().split())) for _ in range(n)]
dp = [[0] * m for _ in range(n)]
dp[0][0] = z[0][0]
for i in range(1, n):
    dp[i][0] = dp[i-1][0] + z[i][0]
for j in range(1, m):
    dp[0][j] = dp[0][j-1] + z[0][j]
for i in range(1, n):
    for j in range(1, m):
        dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + z[i][j] 
print(dp[n-1][m-1])
```