#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int arr[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] != 0 && j > 0 && i > 0) {
        if (arr[i][j] <= arr[i][j - 1] || arr[i][j] <= arr[i - 1][j]) {
          cout << "-1";
          return 0;
        }
      } else if (arr[i][j] != 0 && i == 0 && j > 0) {
        if (arr[0][j] <= arr[0][j - 1]) {
          cout << "-1";
          return 0;
        }
      } else if (arr[i][j] != 0 && j == 0 && i > 0) {
        if (arr[i][0] <= arr[i - 1][0]) {
          cout << "-1";
          return 0;
        }
      }
    }
  }
  for (int i = n - 2; i > 0; i--) {
    for (int j = m - 2; j > 0; j--) {
      if (arr[i][j] == 0) {
        int check = min(arr[i][j + 1], arr[i + 1][j]) - 1;
        if (check > arr[i - 1][j] && check > arr[i][j - 1]) arr[i][j] = check;
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] > 0)
        sum = sum + arr[i][j];
      else {
        cout << "-1";
        return 0;
      }
    }
  }
  cout << sum;
}
