#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, a[501][501], n, k, m, haha, sum = 0;
  cin >> 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--) {
      haha = min(a[i + 1][j], a[i][j + 1]);
      if (haha == a[i + 1][j]) {
        if (a[i][j] == 0) a[i][j] = a[i + 1][j] - 1;
        if (a[i][j] <= a[i][j - 1] || a[i][j] <= a[i - 1][j]) {
          cout << "-1" << endl;
          return 0;
        }
      } else {
        if (a[i][j] == 0) a[i][j] = a[i][j + 1] - 1;
        if (a[i][j] <= a[i][j - 1] || a[i][j] <= a[i - 1][j]) {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
  }
  for (i = n - 1; i >= 1; i--) {
    for (j = m - 1; j >= 1; j--) {
      if (a[i][j] <= a[i][j - 1] || a[i][j] <= a[i - 1][j]) {
        cout << "-1" << endl;
        return 0;
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      sum += a[i][j];
    }
  }
  cout << sum << endl;
}
