#include <bits/stdc++.h>
using namespace std;
int n, m;
int matrix[505][505];
char copyy[505][505];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> matrix[i][j];
      if (matrix[i][j] == 0) {
        copyy[i][j] = 'X';
      }
    }
  }
  for (int i = n - 1; i > 1; i--) {
    for (int j = m - 1; j > 1; j--) {
      if (copyy[i][j] == 'X') {
        matrix[i][j] = min(matrix[i][j + 1], matrix[i + 1][j]) - 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      bool valid = true;
      if (j - 1 >= 1) {
        if (matrix[i][j - 1] >= matrix[i][j]) {
          valid = false;
        }
      }
      if (j + 1 <= m) {
        if (matrix[i][j + 1] <= matrix[i][j]) {
          valid = false;
        }
      }
      if (i - 1 >= 1) {
        if (matrix[i - 1][j] >= matrix[i][j]) {
          valid = false;
        }
      }
      if (i + 1 <= n) {
        if (matrix[i + 1][j] <= matrix[i][j]) {
          valid = false;
        }
      }
      if (!valid) {
        cout << "-1"
             << "\n";
        return (0);
      }
    }
  }
  int rez = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      rez += matrix[i][j];
    }
  }
  cout << rez << "\n";
}
