#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  int t[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> t[i][j];
  int k = 0;
  for (int i = n - 1; i >= 0; i--)
    for (int j = m - 1; j >= 0; j--) {
      if (t[i][j] == 0) {
        int m = min(t[i][j + 1], t[i + 1][j]) - max(t[i - 1][j], t[i][j - 1]);
        if (m == 0 || m == 1) {
          cout << -1 << endl;
          return 0;
        } else
          t[i][j] = min(t[i + 1][j], t[i][j + 1]) - 1;
      }
      k += t[i][j];
    }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m - 1; ++j) {
      if (t[i][j] >= t[i][j + 1]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      if (t[j][i] >= t[j + 1][i]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << k << endl;
  return 0;
}
