#include using namespace std; #ifdef _RUTHEN #include #else #define show(...) true #endif using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) template using V = vector; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, M; cin >> N >> M; V> A(N, V(M)); rep(i, N) rep(j, M) cin >> A[i][j]; if (N == 1) { cout << 0 << '\n'; return 0; } const ll INF = 1LL << 60; V dp(M, INF); rep(i, M) dp[i] = A[0][i]; for (int i = 1; i < N; i++) { V np(M, INF); ll mn = *min_element(dp.begin(), dp.end()); show(mn); rep(j, M) np[j] = mn + A[i - 1][j] + A[i][j]; rep(j, M) np[j] = min(np[j], dp[j] + A[i][j]); swap(dp, np); show(dp); } ll ans = *min_element(dp.begin(), dp.end()); cout << ans << '\n'; return 0; }