#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, sum = 0, x, y, b;
  cin >> n >> m;
  int a[n][m];
  vector<pair<int, int>> v;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> a[i][j];
      sum += a[i][j];
      if (a[i][j] == 0) v.push_back(make_pair(i, j));
    }
  }
  for (i = v.size() - 1; i >= 0; i--) {
    x = v[i].first;
    y = v[i].second;
    a[x][y] = min(a[x + 1][y], a[x][y + 1]);
    a[x][y] -= 1;
    sum += a[x][y];
  }
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < m - 1; j++) {
      if (a[i][j] >= a[i + 1][j] || a[i][j] >= a[i][j + 1]) {
        cout << -1;
        return 0;
      }
    }
  }
  for (i = 0; i < m - 1; i++) {
    if (a[n - 1][i] >= a[n - 1][i + 1]) {
      cout << -1;
      return 0;
    }
  }
  for (j = 0; j < n - 1; j++) {
    if (a[j][m - 1] >= a[j + 1][m - 1]) {
      cout << -1;
      return 0;
    }
  }
  cout << sum;
}
