#include <bits/stdc++.h>
using namespace std;
const int maxn = 8010;
int a[maxn][maxn];
int main() {
  int n, m;
  cin >> n >> m;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      sum += a[i][j];
    }
  }
  int flag = 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 + 1][j], a[i][j + 1]) - 1;
        sum += a[i][j];
      }
      if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1]) {
        flag = 1;
        break;
      }
    }
  }
  for (int i = n - 1; i > 0; i--) {
    for (int j = m - 1; j > 0; j--) {
      if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1]) {
        flag = 1;
        break;
      }
    }
  }
  if (flag)
    cout << "-1" << endl;
  else
    cout << sum << endl;
}
