#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, y;
  cin >> x >> y;
  int arr[x][y];
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      cin >> arr[i][j];
    }
  }
  bool mn = true;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      if (arr[i][j] == 0) {
        mn = false;
        break;
      }
    }
  }
  if (mn) {
    bool m = false;
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y - 1; j++) {
        if (arr[i][j] >= arr[i][j + 1]) {
          m = true;
          break;
        };
      }
    }
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y - 1; j++) {
        if (arr[j][i] >= arr[j + 1][i]) {
          m = true;
          break;
        };
      }
    }
    if (m) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = x - 1; i >= 0; i--) {
    for (int j = y - 1; j >= 0; j--) {
      if (arr[i][j] == 0) {
        int a = arr[i + 1][j];
        int b = arr[i - 1][j];
        int c = arr[i][j + 1];
        int d = arr[i][j - 1];
        bool gu = true;
        int count = 0;
        while (c--) {
          if (d == 0) {
            count++;
          } else if (c > d) {
            count++;
          }
          if (a == 0) {
            count++;
          } else if (c < a) {
            count++;
          }
          if (b == 0) {
            count++;
          } else if (c > b) {
            count++;
          }
          if (count == 3) {
            gu = false;
            break;
          }
          count = 0;
        }
        if (gu) {
          cout << -1 << endl;
          return 0;
        } else {
          arr[i][j] = c;
        }
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      sum += arr[i][j];
    }
  }
  cout << sum << endl;
  return 0;
}
