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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

int A[505][505];
int B[505][505];

const int INF = 1001001001;
int dp[505][505];
struct State {
  int r, c;
  int cost;
  bool operator < (const State& o) const {
    return cost > o.cost;
  }
  int &ref() {
    return dp[r][c];
  }
};

int main2() {
  int R = nextLong();
  int C = nextLong();
  REP(i, R) REP(j, C-1) A[i][j] = nextLong();
  REP(i, R-1) REP(j, C) B[i][j] = nextLong();

  REP(i, R) REP(j, C) dp[i][j] = INF;
  priority_queue<State> pq;

  {
    State s = {0, 0, 0};
    pq.push(s);
    s.ref() = 0;
  }
  while (!pq.empty()) {
    auto cur = pq.top(); pq.pop();
    if (cur.r == R - 1 && cur.c == C - 1){
      break;
    }
    if (cur.cost != cur.ref()) continue;

    if (cur.c + 1 < C) {
      int nr = cur.r;
      int nc = cur.c + 1;
      int n_cost = cur.cost + A[cur.r][cur.c];
      if (dp[nr][nc] > n_cost) {
        dp[nr][nc] = n_cost;
        pq.push((State){nr, nc, n_cost});
      }
    }
    if (cur.c - 1 >= 0) {
      int nr = cur.r;
      int nc = cur.c - 1;
      int n_cost = cur.cost + A[cur.r][cur.c - 1];
      if (dp[nr][nc] > n_cost) {
        dp[nr][nc] = n_cost;
        pq.push((State){nr, nc, n_cost});
      }
    }
    if (cur.r + 1 < R) {
      int nr = cur.r + 1;
      int nc = cur.c;
      int n_cost = cur.cost + B[cur.r][cur.c];
      if (dp[nr][nc] > n_cost) {
        dp[nr][nc] = n_cost;
        pq.push((State){nr, nc, n_cost});
      }
    }
    for (int i = 1; i <= cur.r; i++) {
      int nr = cur.r - i;
      int nc = cur.c;
      int n_cost = cur.cost + (1 + i);
      if (dp[nr][nc] > n_cost) {
        dp[nr][nc] = n_cost;
        pq.push((State){nr, nc, n_cost});
      }
    }


  }

  ll ans = dp[R-1][C-1];
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
#include <bits/stdc++.h>
using namespace std;

int f(int x){
	int ans=0;
	while (x){
		ans+=x%10;
		x/=10;
	}
	return ans;
}

int main(){
	int a,b;
	cin>>a>>b;
	printf("%d\n",max(f(a),f(b)));
	return 0;
}