#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j;
  long long int sum = 0, x;
  cin >> n >> m;
  int a[n][m];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (i = n - 2; i >= 1; i--) {
    for (j = m - 2; j >= 1; j--) {
      if (a[i][j] == 0) {
        x = min(a[i][j + 1], a[i + 1][j]);
        a[i][j] = x - 1;
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m - 1; j++) {
      if (a[i][j] >= a[i][j + 1]) {
        cout << "-1" << endl;
        return 0;
      }
    }
  }
  for (i = 0; i < m; i++) {
    for (j = 0; j < n - 1; j++) {
      if (a[j][i] >= a[j + 1][i]) {
        cout << "-1" << endl;
        return 0;
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      sum = sum + a[i][j];
    }
  }
  cout << sum << endl;
}
