#include <bits/stdc++.h>
using namespace std;
const int N = 5000005;
int r, c, b;
vector<int> ps[N], d[N], g[N];
vector<bool> v[N];
int ec;
pair<int, int> q[N];
int qs, qe;
bool check(int sz) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
bool isBlock = true;
if (i + sz > r || j + sz > c) isBlock = true;
else isBlock = ps[i + sz][j + sz] - ps[i][j + sz] - ps[i + sz][j] + ps[i][j];
g[i][j] = isBlock;
}
}
for (int i = 0; i <= r; i++) {
for (int j = 0; j <= c; j++) {
d[i][j] = 0;
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (!g[i][j]) {
d[i][j]++;
d[i + sz][j]--;
d[i][j + sz]--;
d[i + sz][j + sz]++;
}
}
}
for (int i = 0; i <= r; i++) {
for (int j = 0; j <= c; j++) {
if (i < r) d[i + 1][j] += d[i][j];
if (j < c) d[i][j + 1] += d[i][j];
if (i < r && j < c) d[i + 1][j + 1] -= d[i][j];
}
}
int nec = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
nec += !!d[i][j];
}
}
if (nec != ec) return false;
int eCells = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
eCells += !g[i][j];
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (g[i][j]) continue;
bfs(i, j);
return qe == eCells;
}
}
return false;
}
void bfs(int sx, int sy) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
v[i][j] = false;
}
}
v[sx][sy] = true;
qs = qe = 0;
q[qe++] = {sx, sy};
const pair<int, int> D[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
while (qs < qe) {
auto [cx, cy] = q[qs++];
for (auto [dx, dy] : D) {
int nx = cx + dx, ny = cy + dy;
if (nx < 0 || nx >= r || ny < 0 || ny >= c || v[nx][ny] || g[nx][ny]) continue;
v[nx][ny] = true;
q[qe++] = {nx, ny};
}
}
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> r >> c >> b;
for (int i = 0; i <= r; i++) {
ps[i].resize(c + 1);
d[i].resize(c + 1);
g[i].resize(c + 1);
v[i].resize(c + 1);
}
ec = r * c - b;
while (b--) {
int x, y;
cin >> x >> y;
ps[x][y] = 1;
}
for (int i = 0; i <= r; i++) {
for (int j = 0; j <= c; j++) {
if (i < r) ps[i + 1][j] += ps[i][j];
if (j < c) ps[i][j + 1] += ps[i][j];
if (i < r && j < c) ps[i + 1][j + 1] -= ps[i][j];
}
}
int low = 0, high = min(r, c);
while (low < high) {
int mid = (low + high + 1) / 2;
bool result = check(mid);
if (result) low = mid;
else high = mid - 1;
}
if (low == 0) cout << "-1\n";
else cout << low << '\n';
}