#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long mod = 1e9 + 7;
const long long modd = 1;
const long long N = 1e5 + 5;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long a[n + 1][m + 1];
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) cin >> a[i][j];
  for (long long i = 0; i < n; i++) a[i][m] = a[i][m - 1] + 1;
  for (long long i = 0; i < m; i++) a[n][i] = a[n - 1][i] + 1;
  long long ans = 0;
  for (long long i = n - 1; i >= 0; i = i - 1) {
    for (long long j = m - 1; j >= 0; j = j - 1) {
      if (a[i][j] == 0) a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
      if (a[i][j] >= a[i + 1][j] || a[i][j] >= a[i][j + 1]) {
        cout << -1;
        return;
      }
      ans = ans + a[i][j];
    }
  }
  cout << ans;
}
int32_t main() {
  std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long t = 1;
  for (long long i = 1; i <= t; i = i + 1) {
    solve();
    cout << endl;
  }
}
