#include <bits/stdc++.h>
using namespace std;
int a[501][501];
int main() {
  int n, m, x, y;
  bool b = false;
  long long sum = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 0) {
        x = a[i][j + 1] - 1;
        a[i][j] = x;
        b = true;
      }
      if (((j != 0) && a[i][j] <= a[i][j - 1])) {
        cout << -1 << endl;
        return 0;
      }
      if ((i != 0) && a[i][j] <= a[i - 1][j]) {
        cout << -1 << endl;
        return 0;
      }
      if ((b) && (a[i][j] >= a[i + 1][j])) {
        x = a[i][j] - a[i + 1][j];
        y = a[i][j] - x - 1;
        if (y >= a[i][j + 1] || y <= a[i][j - 1]) {
          cout << -1;
          return 0;
        }
        if (y >= a[i + 1][j] || y <= a[i - 1][j]) {
          cout << -1;
          return 0;
        }
        a[i][j] = y;
      }
      sum += a[i][j];
    }
  }
  cout << sum;
  return 0;
}
