#include <bits/stdc++.h>
using namespace std;
int main() {
  int row, col;
  cin >> row >> col;
  int arr[row][col];
  int flag = 0, total = 0;
  for (int i = 0; i < row; i++) {
    for (int j = 0; j < col; j++) {
      cin >> arr[i][j];
      total += arr[i][j];
    }
  }
  for (int i = row - 2; i > 0; i--) {
    for (int j = col - 2; j > 0; j--) {
      if (arr[i][j] == 0) {
        arr[i][j] = min(arr[i + 1][j], arr[i][j + 1]) - 1;
        if (arr[i][j] == arr[i - 1][j] || arr[i][j] == arr[i][j - 1]) {
          cout << -1 << endl;
          return 0;
        }
        total += arr[i][j];
      } else if (arr[i][j] >= arr[i + 1][j] || arr[i][j] >= arr[i][j + 1] ||
                 arr[i][j] >= arr[i + 1][j + 1]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  if (arr[row - 1][col - 1] <= arr[row - 1][col - 2] ||
      arr[row - 1][col - 1] <= arr[row - 2][col - 1])
    cout << -1 << endl;
  else
    cout << total << endl;
  return 0;
}
