#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,m;
    cin>>n>>m;
    int a[n][m];
    bool pr4=true;
    long long zb=0;
    for (int i=0; i<n; ++i){
        for (int j=0; j<m; ++j){
            cin>>a[i][j];
            zb+=a[i][j];
            if (a[i][j]>1){pr4=false;}
        }
    }
    if (pr4){
        int pm[n][m];
        memset(pm, 0, sizeof(pm));
        pm[0][0]=a[0][0];
        for (int i=1; i<m; ++i){
            pm[0][i]=a[0][i]+pm[0][i-1];
        }
        for (int j=1; j<n; ++j){
            pm[j][0]=a[j][0]+pm[j-1][0];
        }
        for (int i=1; i<n; ++i){
            for (int j=1; j<m; ++j){
                pm[i][j]=max(pm[i-1][j], pm[i][j-1])+a[i][j];
            }
        }
        int x = pm[n-1][m-1];
        cout<<min(zb-2*x+n+m-1,zb);
    }
    else{cout<<zb;}
    return 0;
}