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