#include <bits/stdc++.h>
using namespace std;
const long long N = 600;
long long n, m;
long long a[N][N];
signed main() {
  scanf("%lld%lld", &n, &m);
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      scanf("%lld", &a[i][j]);
    }
  }
  bool f = false;
  for (long long i = n; i >= 1; i--) {
    if (f) break;
    for (long long j = m; j >= 1; j--) {
      if (a[i][j] == 0 && i + 1 <= n && j + 1 <= m) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
      } else {
        if ((i + 1 <= n && a[i + 1][j] <= a[i][j] ||
             j + 1 <= m && a[i][j + 1] <= a[i][j])) {
          f = true;
          break;
        }
      }
    }
  }
  if (f)
    printf("-1\n");
  else {
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= m; j++) {
        sum += a[i][j];
      }
    }
    printf("%lld\n", sum);
  }
  return 0;
}
