#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  srand(time(NULL));
  ;
  long long t, n, m, d;
  t = 1;
  while (t--) {
    cin >> n >> m;
    long long sum = 0;
    vector<vector<long long>> v(n, vector<long long>(m));
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        cin >> v[i][j];
      }
    }
    bool flag = false;
    for (long long i = n - 1; i >= 0; i--) {
      for (long long j = m - 1; j >= 0; j--) {
        if (v[i][j] == 0) {
          long long c = min(v[i][j + 1], v[i + 1][j]) - 1;
          v[i][j] = c;
        }
        if (i != 0 && j != 0) {
          if (v[i][j] <= v[i - 1][j] || v[i][j] <= v[i][j - 1]) {
            flag = true;
            break;
          }
        } else if (i == 0 && j != 0) {
          if (v[i][j] <= v[i][j - 1]) {
            flag = true;
            break;
          }
        } else if (j == 0 && i != 0) {
          if (v[i][j] <= v[i - 1][j]) {
            flag = true;
            break;
          }
        }
        sum += v[i][j];
      }
      if (flag) break;
    }
    if (flag)
      cout << -1 << endl;
    else
      cout << sum << endl;
  }
  return 0;
}
