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

int main() {
  int h,w;
  cin >> h >> w;
  vector<int> a(h*w);
  for(int i=0;i<h*w;i++){
    cin >> a.at(i);
  }
  int b;
  b=*min_element(a.begin(),a.end());
  int c;
  c=0;
  for(int i=0;i<h*w;i++){
    c += a.at(i)-b;
  }
  cout << c << endl;
}

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)

// string->num
//   stoll(str)
// num->string
//   to_string(i)

int main() {
  ll H, W;
  cin >> H >> W;
  vector<vector<ll>> A(H, vector<ll>(W - 1));
  vector<vector<ll>> B(H - 1, vector<ll>(W));
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W - 1; j++) {
      cin >> A[i][j];
    }
  }
  for (ll i = 0; i < H - 1; i++) {
    for (ll j = 0; j < W; j++) {
      cin >> B[i][j];
    }
  }
  const ll INF = 1e10;
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<>> q;
  vector<ll> dist(H * W, INF);

  auto push = [&](ll i, ll j, ll cost) {
    ll node = i * W + j;
    if (dist[node] > cost) {
      dist[node] = cost;
      q.emplace(cost, node);
    }
  };
  push(0, 0, 0);
  while (!q.empty()) {
    auto [cost, node] = q.top();
    q.pop();
    if (dist[node] != cost) continue;
    ll i = node / W;
    ll j = node % W;
    if (j + 1 < W) push(i, j + 1, cost + A[i][j]);
    if (j - 1 >= 0) push(i, j - 1, cost + A[i][j - 1]);
    if (i + 1 < H) push(i + 1, j, cost + B[i][j]);
    for (ll k = 0; k < i; k++) {
      push(k, j, cost + (i - k) + 1);
    }
  }
  cout << dist.back() << endl;
  return 0;
}
