#include <bits/stdc++.h>
using namespace std;
inline void filee() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
int main() {
  int n, m;
  cin >> m >> n;
  int sum = 0;
  int a[m][n];
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
      sum += a[i][j];
    }
  }
  for (int i = m - 1; i >= 1; i--) {
    for (int j = n - 1; j >= 1; j--) {
      if (a[i][j] == 0) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
        sum += a[i][j];
      }
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 1; j < n; j++) {
      if (a[i][j] <= a[i][j - 1]) {
        cout << -1;
        return 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (a[j][i] <= a[j - 1][i]) {
        cout << -1;
        return 0;
      }
    }
  }
  cout << sum;
  return 0;
}
