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