#include <bits/stdc++.h>
using namespace std;
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int main() {
  int n, m;
  cin >> n >> m;
  int mx = 0;
  int mn = 0;
  vector<vector<int>> v(n, vector<int>(m));
  bool b = true;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> v[i][j];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (v[i][j] == 0) {
        mx = min(v[i][j + 1], v[i + 1][j]);
        mn = max(v[i][j - 1], v[i - 1][j]);
        if (mx - mn > 1)
          v[i][j] = mx - 1;
        else {
          b = false;
          break;
        }
      } else {
        if (i == 0 && j == 0) {
          mx = min(v[i][j + 1], v[i + 1][j]);
          if (v[i][j] >= mx) {
            b = false;
            break;
          }
        }
        if (i != 0 && i != n - 1 && j == 0) {
          mn = v[i - 1][j];
          mx = min(v[i][j + 1], v[i + 1][j]);
          if (v[i][j] >= mx || v[i][j] <= mn) {
            b = false;
            break;
          }
        }
        if (i == n - 1 && j == 0) {
          mn = v[i - 1][j];
          mx = v[i][j + 1];
          if (v[i][j] >= mx || v[i][j] <= mn) {
            b = false;
            break;
          }
        }
        if (i == n - 1 && j != 0 && j != m - 1) {
          mn = max(v[i][j - 1], v[i - 1][j]);
          mx = v[i][j + 1];
          if (v[i][j] >= mx || v[i][j] <= mn) {
            b = false;
            break;
          }
        }
        if (i == n - 1 && j == m - 1) {
          mn = max(v[i][j - 1], v[i - 1][j]);
          if (v[i][j] <= mn) {
            b = false;
            break;
          }
        }
        if (i != 0 && i != n - 1 && j == m - 1) {
          mn = max(v[i][j - 1], v[i - 1][j]);
          mx = v[i + 1][j];
          if (v[i][j] >= mx || v[i][j] <= mn) {
            b = false;
            break;
          }
        }
        if (i == 0 && j == m - 1) {
          mn = v[i][j - 1];
          mx = v[i + 1][j];
          if (v[i][j] >= mx || v[i][j] <= mn) {
            b = false;
            break;
          }
        }
        if (i == 0 && j != 0 && j != m - 1) {
          mn = v[i][j - 1];
          mx = min(v[i][j + 1], v[i + 1][j]);
          if (v[i][j] >= mx || v[i][j] <= mn) {
            b = false;
            break;
          }
        }
      }
    }
    if (b == false) break;
  }
  if (b == false)
    cout << -1;
  else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        sum += v[i][j];
      }
    }
    cout << sum;
  }
}
