#pragma ide diagnostic ignored "misc-no-recursion"
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef double db;
typedef complex<db> pt;
#define TESTCASE 0
static constexpr int MOD = 998'244'353;
static constexpr int INF = 1.1e9;
static int N, M, K;
static vector<pair<int, int>> C;
static vector<vector<int>> P1, P2;
static vector<vector<int>> vis;
static void reset(vector<vector<int>> &P) {
for (auto &p: P) {
fill(p.begin(), p.end(), 0);
}
}
static void precalc(vector<vector<int>> &P) {
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
if (x > 0 && y > 0) P[x][y] -= P[x - 1][y - 1];
if (x > 0) P[x][y] += P[x - 1][y];
if (y > 0) P[x][y] += P[x][y - 1];
}
}
}
static void add2(vector<vector<int>> &P, int x, int y, int v) {
if (x < N && y < M) {
P[max(x, 0)][max(y, 0)] += v;
}
}
static void add(vector<vector<int>> &P, int x, int y, int l) {
add2(P, x + 0, y + 0, 1);
add2(P, x + 0, y + l, -1);
add2(P, x + l, y + 0, -1);
add2(P, x + l, y + l, 1);
}
static bool check(int L) {
reset(P1);
reset(P2);
reset(vis);
for (auto [x, y]: C) {
add(P1, x - L + 1, y - L + 1, L);
}
precalc(P1);
queue<pair<int, int>> Q;
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
if (!P1[x][y]) {
Q.emplace(x, y);
break;
}
}
if (!Q.empty()) break;
}
while (!Q.empty()) {
auto [x, y] = Q.front();
Q.pop();
if (vis[x][y]) continue;
vis[x][y] = true;
if (P1[x][y]) continue;
add(P2, x, y, L);
array dx = {0, 0, 1, -1};
array dy = {1, -1, 0, 0};
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
Q.emplace(nx, ny);
}
}
}
for (auto [x, y]: C) {
add(P2, x, y, 1);
}
precalc(P2);
int v = 0;
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
v += !P2[x][y];
}
}
return !v;
}
static void solve([[maybe_unused]] int tc) {
cin >> N >> M >> K;
vis.assign(N, vector<int>(M));
P1.assign(N, vector<int>(M));
P2.assign(N, vector<int>(M));
for (int i = 0; i < K; i++) {
int x, y;
cin >> x >> y;
C.emplace_back(x - 1, y - 1);
}
for (int x = 0; x < N; x++) {
C.emplace_back(x, M);
}
for (int y = 0; y < M; y++) {
C.emplace_back(N, y);
}
int l = 0, r = min(N, M) + 1;
while (r - l > 1) {
int m = (l + r) / 2;
print(l, m, r);
if (check(m)) {
l = m;
} else {
r = m;
}
}
cout << (l ? l : -1) << endl;
}
int main() {
ios::sync_with_stdio(false);
if (const char *f = getenv("REDIRECT_STDOUT"); f) {
freopen(f, "w", stdout);
}
int T = 1;
#if TESTCASE
cin >> T;
#endif
for (int t = 1; t <= T; t++) {
solve(t);
}
return 0;
}