#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  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 flag = 0;
  for (int i = n - 2; i >= 1; i--) {
    for (int j = m - 2; j >= 1; j--) {
      if ((a[i + 1][j] == a[i][j - 1]) || (a[i][j + 1] == a[i - 1][j])) {
        flag = 1;
        break;
      } else if (a[i][j] == 0) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
      }
      if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1]) {
        flag = 1;
        break;
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i][0] >= a[i + 1][0] || a[i][m - 1] >= a[i + 1][m - 1]) {
      flag = 1;
      break;
    }
  }
  for (int i = 0; i < m - 1; i++) {
    if (a[0][i] >= a[0][i + 1] || a[n - 1][i] >= a[n - 1][i + 1]) {
      flag = 1;
      break;
    }
  }
  if (flag == 1) {
    cout << -1 << "\n";
    return 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) sum += a[i][j];
  }
  cout << sum;
  return 0;
}
