#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 5;
void solve() {
  long long int n, m;
  cin >> n >> m;
  long long int a[n][m];
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (long long int i = n - 2; i >= 1; i--) {
    for (long long int j = m - 2; j >= 1; j--) {
      if (a[i][j] == 0) {
        a[i][j] = min(a[i][j + 1] - 1, a[i + 1][j] - 1);
        if (a[i][j] <= max(a[i][j - 1], a[i - 1][j])) {
          cout << "-1"
               << "\n";
          return;
        }
      }
    }
  }
  for (long long int i = 0; i < n - 1; i++) {
    if (a[i][0] > a[i + 1][0]) {
      cout << "-1"
           << "\n";
      return;
    }
  }
  for (long long int j = 0; j < m - 1; j++) {
    if (a[0][j] > a[0][j + 1]) {
      cout << "-1"
           << "\n";
      return;
    }
  }
  long long int sum = 0;
  for (long long int i = 1; i < n; i++) {
    for (long long int j = 1; j < m; j++) {
      if (a[i][j] <= max(a[i][j - 1], a[i - 1][j])) {
        cout << "-1"
             << "\n";
        return;
      }
    }
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m; j++) {
      sum = sum + a[i][j];
    }
  }
  cout << sum << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) solve();
  return 0;
}
