#include <bits/stdc++.h>
using namespace std;
int a[512][512];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int s = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      s += a[i][j];
    }
  }
  for (int i = n - 2; i > 0; i--) {
    for (int j = m - 2; j > 0; j--) {
      int mn = min(a[i][j + 1], a[i + 1][j]) - 1;
      if (mn <= 0) {
        return printf("-1"), 0;
      }
      if (!a[i][j]) {
        a[i][j] = mn;
        s += mn;
      }
    }
  }
  bool ok = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m - 1; j++) {
      if (a[i][j] >= a[i][j + 1]) ok = false;
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n - 1; j++) {
      if (a[j][i] >= a[j + 1][i]) ok = false;
    }
  }
  if (!ok) return printf("-1"), 0;
  printf("%d", s);
  return 0;
}
