#include using ll = long long; using std::cin; using std::cout; using std::endl; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const int inf = (int)1e9 + 7; const long long INF = 1LL << 60; void solve([[maybe_unused]] int CASE) { int n, m, q; cin >> n >> m >> q; std::vector> vp(q); std::vector> mat(n, std::vector(m)); for (int i = 0; i < q; ++i) { cin >> vp[i].first >> vp[i].second; vp[i].first -= 1, vp[i].second -= 1; mat[vp[i].first][vp[i].second] = 1; } std::vector dp(4, std::vector(n, std::vector(m))); if (mat[0][0]) dp[3][0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (i + 1 < n) { for (int S = 0; S < 4; ++S) { int nS = S; if (S >> 1 & 1) nS -= (1 << 1); chmax(dp[nS][i + 1][j], dp[S][i][j]); if (mat[i + 1][j] and nS == 0) { chmax(dp[3][i + 1][j], dp[S][i][j] + 1); } } } if (j + 1 < m) { for (int S = 0; S < 4; ++S) { int nS = S; if (S & 1) nS -= 1; chmax(dp[nS][i][j + 1], dp[S][i][j]); if (mat[i][j + 1] and nS == 0) { chmax(dp[3][i][j + 1], dp[S][i][j] + 1); } } } } } // for (int i = 0; i < n; ++i) // { // for (int j = 0; j < m; ++j) // { // cout << dp[3][i][j] << " \n"[j + 1 == m]; // } // } int res = 0; for (int i = 0; i < 4; ++i) chmax(res, dp[i][n - 1][m - 1]); cout << res << endl; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int kkt = 1; // cin >> kkt; for (int jupi = 1; jupi <= kkt; jupi++) solve(jupi); return 0; }