#include #include #include #include #include #include #include #include #include #include #include #include #define PI 3.14159265358979323846 #define mod 1000000007 #define pow9 1000000000 #define INF (1 << 30) using namespace std; int main() { int h, w; cin >> h >> w; vector> s(h, vector(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> s[i][j]; } } vector ans(h * w + 1); ans[1] = s[0][0]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (i == 0 && j == 0) continue; else if (i == 0) { ans[i * w + j + 1] = ans[i * w + j] + s[i][j]; } else if (j == 0) { ans[i * w + j + 1] = ans[(i - 1) * w + j + 1] + s[i][j]; } else if (ans[(i - 1) * w + j + 1] >= ans[i * w + j]) { ans[i * w + j + 1] = ans[i * w + j] + s[i][j]; } else { ans[i * w + j + 1] = ans[(i - 1) * w + j + 1] + s[i][j]; } } } cout << ans[h * w] << endl; }