#include <bits/stdc++.h>
#define re register
#define get(x,y) (m * (x - 1) + y)
using namespace std;
const int N = 3e6 + 10;
int n,m,k,typ,L;
int dx[] = {0,1,-1,0,0};
int dy[] = {0,0,0,1,-1};
int sum[N],c[N];
bool st[N],vis[N];
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
inline void dfs1(int x,int y){
vis[get(x,y)] = true;
for (re int i = 1;i <= 4;i++){
int tx = x + dx[i];
int ty = y + dy[i];
if (1 <= tx && tx <= n && 1 <= ty && ty <= m && !st[get(tx,ty)] && !vis[get(tx,ty)]) dfs1(tx,ty);
}
}
inline int getsum(int x,int y,int xx,int yy){
int tmp = sum[get(xx,yy)];
if (x > 1) tmp -= sum[get(x - 1,yy)];
if (y > 1) tmp -= sum[get(xx,y - 1)];
if (x > 1 && y > 1) tmp += sum[get(x - 1,y - 1)];
return tmp;
}
inline void dfs2(int x,int y){
vis[get(x,y)] = true;
int xx = x + L - 1,yy = y + L - 1;
// cerr << x << " " << y << " " << xx << " " << yy << " ???\n";
c[get(x,y)]++;
if (xx + 1 <= n && yy + 1 <= m) c[get(xx + 1,yy + 1)]++;
if (xx + 1 <= n) c[get(xx + 1,y)]--;
if (yy + 1 <= m) c[get(x,yy + 1)]--;
for (re int i = 1;i <= 4;i++){
int tx = x + dx[i];
int ty = y + dy[i];
if (1 <= tx && tx <= n && 1 <= ty && ty <= m && !vis[get(tx,ty)]){
int hx = tx + L - 1;
int hy = ty + L - 1;
if (1 <= hx && hx <= n && 1 <= hy && hy <= m && !getsum(tx,ty,hx,hy)) dfs2(tx,ty);
}
}
}
inline bool check(int len){
L = len;
fill(c + 1,c + n * m + 1,0);
fill(vis + 1,vis + n * m + 1,false);
for (re int i = 1;i + len - 1 <= n;i++){
for (re int j = 1;j + len - 1 <= m;j++){
int x = i + len - 1,y = j + len - 1;
if (!getsum(i,j,x,y)){
dfs2(i,j); goto End;
}
}
}
End:;
// for (re int i = 1;i <= n;i++){
// for (re int j = 1;j <= m;j++) cerr << c[get(i,j)] << " ";
// cerr << "\n";
// }
for (re int i = 1;i <= n;i++){
for (re int j = 1;j <= m;j++){
int tmp = c[get(i,j)];
if (i > 1) tmp += c[get(i - 1,j)];
if (j > 1) tmp += c[get(i,j - 1)];
if (i > 1 && j > 1) tmp -= c[get(i - 1,j - 1)];
// cerr << i << " " << j << " " << tmp << " ???\n";
c[get(i,j)] = tmp;
// cerr << i << " " << j << " " << c[get(i,j)] << " " << c[get(i - 1,j)] << " " << c[get(i,j - 1)] << " " << c[get(i - 1,j - 1)] << " ???\n";
if (!st[get(i,j)] && !tmp) return false;
}
// cerr << "\n";
}
return true;
}
int main(){
n = read(),m = read(),k = read();
for (re int i = 1,x,y;i <= k;i++){
x = read(),y = read();
st[get(x,y)] = true;
}
for (re int i = 1;i <= n;i++){
for (re int j = 1;j <= m;j++){
if (!st[get(i,j)]){
dfs1(i,j); goto End;
}
}
}
End:;
for (re int i = 1;i <= n;i++){
for (re int j = 1;j <= m;j++){
if (!st[get(i,j)] && !vis[get(i,j)]) return puts("-1"),void();
}
}
for (re int i = 1;i <= n;i++){
for (re int j = 1;j <= m;j++){
int tmp = st[get(i,j)];
if (i > 1) tmp += sum[get(i - 1,j)];
if (j > 1) tmp += sum[get(i,j - 1)];
if (i > 1 && j > 1) tmp -= sum[get(i - 1,j - 1)];
sum[get(i,j)] = tmp;
}
}
// cout << check(3); return;
int l = 1,r = min(n,m);
while (l < r){
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
printf("%d\n",l);
return 0;
}