#include <bits/stdc++.h>
using namespace std;
const double PI = 3.141592653589793238460;
const long long mod = 1e9 + 7;
void FAST() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
bool comp(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.second);
}
int main() {
  FAST();
  int n, m;
  cin >> n >> m;
  vector<vector<int> > a(n, vector<int>(m, 0));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) cin >> 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] - 1, a[i][j + 1] - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (a[i][j] <= a[i][j - 1]) {
        cout << -1 << '\n';
        return 0;
      }
    }
  }
  for (int j = 0; j < m; j++) {
    for (int i = 1; i < n; i++) {
      if (a[i][j] <= a[i - 1][j]) {
        cout << -1 << '\n';
        return 0;
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) sum += a[i][j];
  cout << sum << '\n';
  return 0;
}
