#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2005;

int n, m;
int a[MAXN][MAXN];
int dp[MAXN][MAXN];

int main() {
    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }


    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            dp[i][j] = a[i][j];
        }
    }


    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (i > 1) {
                dp[i][j] = min(dp[i][j], dp[i-1][j] + a[i][j]);
            }
            if (j > 1) {
                dp[i][j] = min(dp[i][j], dp[i][j-1] + a[i][j]);
            }
        }
    }

    cout << dp[n][m] << endl;

    return 0;
}