#include <bits/stdc++.h>
#define R 3000
#define C 3000
int points[3000][3000];
int dp[3000][3000];
using namespace std;

int minInitialPoints(int points[3000][3000],int m, int n)
{





    dp[m - 1][n - 1] = points[m - 1][n - 1] > 0
                           ? 1
                           : abs(points[m - 1][n - 1]) + 1;


    for (int i = m - 2; i >= 0; i--)
        dp[i][n - 1]
            = max(dp[i + 1][n - 1] - points[i][n - 1], 1);
    for (int j = n - 2; j >= 0; j--)
        dp[m - 1][j]
            = max(dp[m - 1][j + 1] - points[m - 1][j], 1);


    for (int i = m - 2; i >= 0; i--) {
        for (int j = n - 2; j >= 0; j--) {
            int min_points_on_exit
                = min(dp[i + 1][j], dp[i][j + 1]);
            dp[i][j]
                = max(min_points_on_exit - points[i][j], 1);
        }
    }

    return dp[0][0];
}


int main()
{


    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>points[i][j];
           points[i][j]=0-points[i][j];
        }
    }


    cout << minInitialPoints(points,n,m)-1;
    return 0;
}