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