#include <bits/stdc++.h>
using namespace std;
int matrix[505][505];
int check1[505][505];
int check2[505][505];
int main() {
  int row, column, i, j, k, sum = 0, sum1 = 0;
  cin >> row;
  cin >> column;
  for (i = 0; i < row; i++) {
    for (j = 0; j < column; j++) {
      cin >> matrix[i][j];
      check1[i][j] = check2[i][j] = matrix[i][j];
      sum = sum + matrix[i][j];
    }
  }
  {
    for (j = column - 1; j >= 0; j = j - 1) {
      if (check1[i][j] != 0)
        k = check1[i][j];
      else {
        check1[i][j] = k - 1;
        k = k - 1;
      }
    }
  }
  for (j = column - 1; j >= 0; j = j - 1) {
    for (i = row - 1; i >= 0; i = i - 1) {
      if (check2[i][j] != 0)
        k = check2[i][j];
      else {
        check2[i][j] = k - 1;
        k = k - 1;
      }
    }
  }
  for (j = column - 1; j >= 0; j = j - 1) {
    for (i = row - 1; i >= 0; i = i - 1) {
      if (matrix[i][j] == 0) {
        check1[i][j] = check1[i][j + 1] - 1;
        if (check1[i][j] > check2[i][j]) check1[i][j] = check2[i][j];
      }
    }
  }
  for (i = 0; i < row; i++) {
    for (j = 0; j < column; j++) {
      if (j != column - 1) {
        if (check1[i][j] >= check1[i][j + 1]) {
          sum = 0;
          goto print;
        }
      }
      if (i != row - 1) {
        if (check1[i][j] >= check1[i + 1][j]) {
          sum = 0;
          goto print;
        }
      }
      sum1 = sum1 + check1[i][j];
    }
  }
print:
  if (sum == 0)
    cout << "-1";
  else
    cout << sum1;
  return 0;
}
