#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  int cost = 0;
  for (int i = n - 2; i > 0; i--) {
    for (int j = m - 2; j > 0; j--) {
      if (a[i][j] == 0) {
        a[i][j] = min(a[i][j + 1], a[i + 1][j]) - 1;
      }
    }
  }
  int i, j, flag = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m - 1; j++) {
      if (a[i][j] >= a[i][j + 1]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (i = 0; i < m; i++) {
    for (j = 0; j < n - 1; j++) {
      if (a[j][i] >= a[j + 1][i]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cost += a[i][j];
    }
  }
  cout << cost;
  return 0;
}
