#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, sum = 0, low, upp, flag = 0;
  cin >> n >> m;
  int a[n + 1][m + 1];
  int i, j;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  for (i = n - 1; i > 1; i--) {
    for (j = m - 1; j > 1; j--) {
      upp = min(a[i][j + 1], a[i + 1][j]);
      if (a[i - 1][j] == 0 && a[i][j - 1] != 0) {
        low = a[i][j - 1];
      } else if (a[i - 1][j] != 0 && a[i][j - 1] == 0) {
        low = a[i - 1][j];
      } else {
        low = max(a[i - 1][j], a[i][j - 1]);
      }
      if (upp - low > 1 && a[i][j] == 0) {
        a[i][j] = upp - 1;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      sum = sum + a[i][j];
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j < m; j++) {
      if (a[i][j] >= a[i][j + 1]) {
        sum = -1;
      }
    }
  }
  for (j = 1; j <= m; j++) {
    for (i = 1; i < n; i++) {
      if (a[i][j] >= a[i + 1][j]) {
        sum = -1;
      }
    }
  }
  cout << sum;
  return 0;
}
