#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  if (a[n - 1][m - 1] == 0) {
    a[n - 1][m - 1] = 8000;
  }
  long long ans = a[n - 1][m - 1];
  for (int i = n - 2; i >= 0; i--) {
    if (a[i][m - 1] == 0) {
      a[i][m - 1] = a[i + 1][m - 1] - 1;
    } else if (a[i][m - 1] >= a[i + 1][m - 1]) {
      cout << -1;
      return 0;
    }
    ans += a[i][m - 1];
  }
  for (int i = m - 2; i >= 0; i--) {
    if (a[n - 1][i] == 0) {
      a[n - 1][i] = a[n - 1][i + 1] - 1;
    } else if (a[n - 1][i] >= a[n - 1][i + 1]) {
      cout << -1;
      return 0;
    }
    ans += a[n - 1][i];
  }
  for (int i = n - 2; i >= 0; i--) {
    for (int j = m - 2; j >= 0; j--) {
      if (a[i][j] == 0) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
      } else if (a[i][j] >= a[i + 1][j] || a[i][j] >= a[i][j + 1]) {
        cout << -1;
        return 0;
      }
      ans += a[i][j];
    }
  }
  cout << ans;
}
