Go: package main

import "fmt"

func main() {
	//部屋を生成
	var H int

	fmt.Scan(&H)
	room := [][4]bool{}

	for i := 0; i < H; i++ {
		room = append(room, [4]bool{})
	}

	//荷物のおけない区画を設定
	var N int
	fmt.Scan(&N)
	for i := 0; i < N; i++ {
		var x, y int
		fmt.Scan(&x, &y)
		room[y][x] = true //ものがある状態を真とする
	}
	//荷物を配置
	var count int
	for j := 0; j < H-1; j++ {
		for i := 0; i < 3; i++ {

			if !room[j][i] && !room[j+1][i] && !room[j][i+1] && !room[j+1][i+1] {
				if i!=0&&i!=2&&j!=H-3{
					if !room[j+1][i-1]&&!room[j+1][i+2]{
						if !room[j+2][0]&&!room[j+2][1]&&!room[j+2][2]&&!room[j+2][3]{
							break
						}
					}
				}
				room[j][i] = true
				room[j+1][i] = true
				room[j][i+1] = true
				room[j+1][i+1] = true
				count++
			}
		}
	}
	//for _,v:=range room{
	//	for _,v2:=range v{
	//		if !v2 {
	//			fmt.Print("◯")
	//		}else{
	//			fmt.Print("☓")
	//		}
	//	}
	//	fmt.Println()
	//}
	fmt.Println(count)
}

C++: #include "bits/stdc++.h"

using namespace std;
using uint = unsigned int;
using int64 = long long;
using i64 = int64;
using uint64 = unsigned long long;
using vint = vector<int>;
using vint64 = vector<int64>;
template<typename T>
using p_que = priority_queue<T>;
#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rep1(i,N) for(int i=1;i<=(int)(N);++i)
#define fs first
#define sc second
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

const int INF = (1 << 30) - 1;
const long long INF64 = (1ll << 60) - 1;

template<typename T>
vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

template<class T> ostream& operator<<(ostream& os, const vector<T>& v) {
	os << "[";
	for (auto& e : v) os << e << ", ";
	return os << "]";
}

template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U> p) {
	return os << "P(" << p.first << ", " << p.second << ")";
}

template<class T, class U> ostream& operator<<(ostream& os, const map<T, U>& mp) {
	os << "{";
	for (auto& e : mp) os << e << " ";
	return os << "}";
}

void Main();
signed main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);
	Main();
}

/* Cut from here! */

template <uint mod> struct ModInt {
	uint x;
	ModInt(int64_t y = 0) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt& operator+=(const ModInt& p) {
		if ((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt& operator-=(const ModInt& p) {
		x = (int)(1ll * x * p.x % mod);
		return *this;
	}
	ModInt& operator/=(const ModInt& p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const {
		return ModInt(-x);
	}
	ModInt operator+(const ModInt& p) const {
		return ModInt(*this) += p;
	}
	ModInt operator-(const ModInt& p) const {
		return ModInt(*this) -= p;
	}
	ModInt operator*(const ModInt& p) const {
		return ModInt(*this) *= p;
	}
	ModInt operator/(const ModInt& p) const {
		return ModInt(*this) /= p;
	}
	bool operator==(const ModInt& p) const {
		return x == p.x;
	}
	bool operator!=(const ModInt& p) const {
		return x != p.x;
	}
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(int64_t n) const {
		ModInt ret(1), mul(x);
		while (n > 0) {
			if (n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}return ret;
	}
	friend ostream& operator<<(ostream& os, const ModInt& p) {
		return os << p.x;
	}
};

template <typename Monoid> 
struct SegmentTree {
	using F = function<Monoid(Monoid, Monoid)>;
	int sz;
	vector<Monoid> seg;

	const F f;
	const Monoid M1;

	SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) {
		sz = 1;
		while (sz < n) sz <<= 1;
		seg.assign(2 * sz, M1);
	}

	void set(int k, const Monoid& x) {
		seg[k + sz] = x;
	}

	void build() {
		for (int k = sz - 1; k > 0; --k) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}

	void update(int k, const Monoid& x) {
		k += sz;
		seg[k] = x;
		while (k >>= 1) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}

	Monoid query(int a, int b) {
		Monoid L = M1, R = M1;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if (a & 1) L = f(L, seg[a++]);
			if (b & 1) R = f(seg[--b], R);
		}
		return f(L, R);
	}

	Monoid operator[](const int& k) const {
		return seg[k + sz];
	}
};

//#define int long long
const uint MOD = 1e9 + 7;

template<typename T>
T gcd(T x, T y) {
	if (x == 0 || y == 0) return 1;
	T r;
	while ((r = y % x) != 0) {
		y = x;
		x = r;
	}
	return x;
}

void Main() {
	int H, N; cin >> H >> N;
	auto dp = make_v(H, 10, 0ll);
	auto G = make_v(H, 4, 0);
	rep(i, N) {
		int x, y; cin >> x >> y;
		G[y][x] = 1;
	}

	rep(i, H - 1) {
		bool f1 = false, f2 = false, f3 = false;
		if (!G[i][0] && !G[i][1] && !G[i + 1][0] && !G[i + 1][1]) f1 = true;
		if (!G[i][1] && !G[i][2] && !G[i + 1][1] && !G[i + 1][2]) f2 = true;
		if (!G[i][2] && !G[i][3] && !G[i + 1][2] && !G[i + 1][3]) f3 = true;

		if (f1) {
			if (i != 0) dp[i][0b100] = max(dp[i - 1][0b000], dp[i - 1][0b001]) + 1;
			else dp[i][0b100] = 1;
		}
		if (f2) {
			if (i != 0) dp[i][0b010] = dp[i - 1][0b000] + 1;
			else dp[i][0b010] = 1;
		}
		if (f3) {
			if (i != 0) dp[i][0b001] = max(dp[i - 1][0b000], dp[i - 1][0b100]) + 1;
			else dp[i][0b001] = 1;
		}
		if (f1 && f3) {
			if (i != 0) dp[i][0b101] = dp[i - 1][0b000] + 2;
			else dp[i][0b101] = 2;
		}
		if (i != 0) dp[i][0b000] = max({ dp[i - 1][0b000], dp[i - 1][0b100], dp[i - 1][0b010], dp[i - 1][0b001], dp[i - 1][0b101] });
		else dp[i][0b000] = 0;
	}

	cout << *max_element(all(dp[H - 2])) << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS = 1e-10;
const bool debug = 0;
//------------------------------//

int H, N;
int x[41234], y[41234];
int dp[41234][1<<4];

int main() {
	cin >> H >> N;
	
	set<pii> ss; // x, y
	REP(i, N) {
		scanf("%d %d", x + i, y + i);
		ss.insert(pii(x[i], y[i]));
	}
	
	memset(dp, -1, sizeof(dp));
	
	dp[0][0] = 0;
	REP(i, H - 1) {
		int b = 0;
		REP(j, 4) if (ss.find(pii(j, i)) != ss.end()) b |= 1<<j;
		
		int sup = 15 ^ b;
		int sub = sup;
		do {
			int j = sup | b;
			if (dp[i][j] != -1) {
				chmax(dp[i + 1][0], dp[i][j]);
				
				int ok = 0;
				REP(k, 4) if (ss.find(pii(k, i + 1)) != ss.end()) ok |= 1<<k;
				
				if ((ok & 3) == 3 && (j & 3) == 3) chmax(dp[i + 1][3], dp[i][j] + 1);
				if ((ok & 6) == 6 && (j & 6) == 6) chmax(dp[i + 1][6], dp[i][j] + 1);
				if ((ok & 12) == 12 && (j & 12) == 12) chmax(dp[i + 1][12], dp[i][j] + 1);
				if ((ok & 15) == 15 && (j & 15) == 15) chmax(dp[i + 1][15], dp[i][j] + 2);
			}
			sub = (sub - 1) & sup;
		} while (sub != sup);
		
		REP(j, 1<<4) { // 1???????????¶???
			if (dp[i][j] == -1 || (j & b) != b) continue;
		}
	}
	
	int ans = 0;
	REP(i, 1<<4) chmax(ans, dp[H][i]);
	cout << ans << endl;
	
	return 0;
}