#include <bits/stdc++.h>
using namespace std;
int sum(vector<int> &numbers) {
  int ansver = 0;
  for (int i : numbers) {
    ansver += i;
  }
  return ansver;
}
int main() {
  int numbLines, numbColumns;
  cin >> numbLines >> numbColumns;
  vector<int> numbers(numbColumns * numbLines, 0);
  for (int i = 0; i < numbLines * numbColumns; i++) {
    cin >> numbers.at(i);
  }
  int currPos = (numbColumns * numbLines) - 2 - numbColumns;
  for (int i = 0; i < numbLines - 2; i++) {
    for (int j = 0; j < numbColumns - 2; j++) {
      if (numbers.at(currPos) == 0) {
        if (max(numbers.at(currPos - 1), numbers.at(currPos - numbColumns)) +
                1 >=
            min(numbers.at(currPos + 1), numbers.at(currPos + numbColumns))) {
          cout << -1 << endl;
          return 0;
        }
        numbers.at(currPos) =
            min(numbers.at(currPos + 1), numbers.at(currPos + numbColumns)) - 1;
      }
      currPos--;
    }
    currPos -= 2;
  }
  currPos = 0;
  for (int j = 0; j < numbLines - 1; j++) {
    for (int i = 0; i < numbColumns - 1; i++) {
      if (numbers.at(currPos) >= numbers.at(currPos + 1) or
          numbers.at(currPos) >= numbers.at(currPos + numbColumns)) {
        cout << -1 << endl;
        return 0;
      }
      currPos++;
    }
    currPos++;
  }
  currPos = numbColumns - 1;
  for (int i = 0; i < numbLines - 1; i++) {
    if (numbers.at(currPos) >= numbers.at(currPos + numbColumns)) {
      cout << -1 << endl;
      return 0;
    }
    currPos += numbColumns;
  }
  currPos = numbLines * numbColumns - numbColumns;
  for (int i = 0; i < numbColumns - 1; i++) {
    if (numbers.at(currPos) >= numbers.at(currPos + 1)) {
      cout << -1 << endl;
      return 0;
    }
    currPos++;
  }
  cout << sum(numbers) << endl;
}
