#include <bits/stdc++.h>
using namespace std;
int main() {
  long long s = 0;
  int n, m;
  cin >> n >> m;
  int a[500][500];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
      s += a[i][j];
    }
  }
  for (int i = n - 1; i > 0; --i) {
    for (int j = m - 1; j > 0; --j) {
      if (a[i][j] == 0) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
        s += a[i][j];
      }
      if (a[i][j] <= max(a[i - 1][j], a[i][j - 1])) {
        cout << -1;
        return 0;
      }
    }
  }
  cout << s;
  return 0;
}
