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