#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
  ll n, m;
  cin >> n >> m;
  ll a[n][m];
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < m; j++) cin >> a[i][j];
  ll ans;
  for (int i = n - 1; i > 0; i--) {
    for (int j = m - 1; j > 0; j--) {
      if (a[i][j] == 0) {
        vector<ll> b(4);
        ans = 0;
        b[0] = a[i - 1][j] + 1;
        b[1] = a[i][j - 1] + 1;
        b[2] = a[i][j + 1] - 1;
        b[3] = a[i + 1][j] - 1;
        for (ll z = 0; z < 4; z++) {
          if (a[i][j + 1] == 0 && a[i + 1][j] == 0) {
            if (b[z] > a[i][j - 1] && b[z] > a[i - 1][j]) {
              ans = max(b[z], ans);
            }
          } else if (a[i][j + 1] == 0) {
            if (b[z] > a[i][j - 1] && b[z] > a[i - 1][j] &&
                b[z] < a[i + 1][j]) {
              ans = max(b[z], ans);
            }
          } else if (a[i + 1][j] == 0) {
            if (b[z] > a[i][j - 1] && b[z] > a[i - 1][j] &&
                b[z] < a[i][j + 1]) {
              ans = max(b[z], ans);
            }
          } else {
            if (b[z] > a[i][j - 1] && b[z] > a[i - 1][j] &&
                b[z] < a[i][j + 1] && b[z] < a[i + 1][j]) {
              ans = max(b[z], ans);
            }
          }
        }
        if (ans == 0) {
          cout << -1;
          exit(0);
        } else {
          a[i][j] = ans;
        }
      }
    }
  }
  ans = 0;
  ll chk = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m - 1; j++) {
      if (a[i][j] >= a[i][j + 1]) {
        chk = 1;
      }
      ans += a[i][j];
    }
    ans += a[i][m - 1];
  }
  for (ll i = 0; i < m; i++) {
    for (ll j = 0; j < n - 1; j++) {
      if (a[j][i] >= a[j + 1][i]) {
        chk = 1;
      }
    }
  }
  if (chk)
    cout << -1;
  else
    cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
