#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vec;
typedef vector<vec> vec2d;

int N, M;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> M;
    vec2d A(N, vec(M));
    vec2d prefLeft(N, vec(M));
    vec2d prefTop(N, vec(M));
    vec2d dp(N, vec(M));

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            cin >> A[i][j];
        }
    }

    for (int i = 0; i < N; i++)
    {
        prefLeft[i][0] = 0;
        int sum1 = 0, sum2 = 0;
        for (int j = 1; j < M; j++)
        {
            sum2 += A[i][j - 1];
            sum1 += sum2;
            prefLeft[i][j] = sum1;
        }
    }

    for (int j = 0; j < M; j++)
    {
        prefTop[0][j] = 0;
        int sum1 = 0, sum2 = 0;
        for (int i = 1; i < N; i++)
        {
            sum2 += A[i - 1][j];
            sum1 += sum2;
            prefTop[i][j] = sum1;
        }
    }

    for (int i = 0; i < N; i++)
    {
        dp[i][0] = 0;
    }

    for (int j = 0; j < M; j++)
    {
        dp[0][j] = 0;
    }

    for (int i = 1; i < N; i++)
    {
        for (int j = 1; j < M; j++)
        {
            dp[i][j] = min(dp[i][j - 1] + prefTop[i][j], dp[i - 1][j] + prefLeft[i][j]);
        }
    }

    cout << dp[N - 1][M - 1] << "\n";

    return 0;
}