#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int tc = 1;
  long long int inM = 8001;
  while (tc--) {
    long long int n, m;
    cin >> n >> m;
    long long int ar[n + 2][m + 2];
    for (long long int i = 0; i <= n + 1; i++) {
      ar[0][i] = 0;
    }
    for (long long int i = 0; i <= n + 1; i++) {
      ar[i][0] = 0;
    }
    for (long long int i = 0; i <= n + 1; i++) {
      ar[i][m + 1] = inM;
    }
    for (long long int i = 0; i <= m + 1; i++) {
      ar[n + 1][i] = inM;
    }
    for (long long int i = 1; i <= n; i++)
      for (long long int j = 1; j <= m; j++) cin >> ar[i][j];
    bool flag = true;
    long long int sum = 0;
    for (long long int i = n; i >= 1; i--) {
      for (long long int j = m; j >= 1; j--) {
        if (ar[i][j] == 0) {
          long long int mn = min(ar[i + 1][j], ar[i][j + 1]);
          mn--;
          long long int mx = max(ar[i - 1][j], ar[i][j - 1]);
          mx++;
          if (mn >= mx) {
            ar[i][j] = mn;
          } else {
            flag = false;
            break;
          }
        }
      }
      if (flag == false) break;
    }
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 1; j <= m; j++) {
        if (ar[i][j] > ar[i - 1][j] && ar[i][j] > ar[i][j - 1])
          sum += ar[i][j];
        else {
          flag = false;
          break;
        }
      }
      if (flag == false) break;
    }
    if (flag)
      cout << sum << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
