#include <bits/stdc++.h>
using namespace std;
#define el '\n'
#define ll long long
#define ld long double
#define ull unsigned long long
#define pll pair<long long, long long>
#define ppll pair< pair<long long, long long>, long long >
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
const ll DIM = 2e5 + 7;
const ll INF = 1e16;
const ll mod = 1e9 + 7;
const ll maxlog = 20;
ll n, m;
vector< vector<ll> > a, vis;
ll x[17], y[17];
ll check(ll x, ll y, ll type) {
    if (type == 0) {
        if (x == n || y == 1 || y == m ||
            vis[x][y] || vis[x][y-1] || vis[x][y+1] || vis[x+1][y]) return -INF;
        vis[x][y] = vis[x][y-1] = vis[x][y+1] = vis[x+1][y] = 1;
        return a[x][y] + a[x][y-1] + a[x][y+1] + a[x+1][y];
    }
    else if (type == 1) {
        if (x == 1 || y == 1 || y == m ||
            vis[x][y] || vis[x-1][y] || vis[x][y-1] || vis[x][y+1]) return -INF;
        vis[x][y] = vis[x-1][y] = vis[x][y-1] = vis[x][y+1] = 1;
        return a[x][y] + a[x-1][y] + a[x][y-1] + a[x][y+1];
    }
    else if (type == 2) {
        if (x == 1 || x == n || y == m ||
            vis[x][y] || vis[x-1][y] || vis[x+1][y] || vis[x][y+1]) return -INF;
        vis[x][y] = vis[x-1][y] = vis[x+1][y] = vis[x][y+1] = 1;
        return a[x][y] + a[x-1][y] + a[x+1][y] + a[x][y+1];
    }
    else {
        if (x == 1 || x == n || y == 1 ||
            vis[x][y] || vis[x-1][y] || vis[x+1][y] || vis[x][y-1]) return -INF;
        vis[x][y] = vis[x-1][y] = vis[x+1][y] = vis[x][y-1] = 1;
        return a[x][y] + a[x-1][y] + a[x+1][y] + a[x][y-1];
    }
}
void solve() {
    ll n, m;
    cin >> n >> m;
    a.resize(n+1, vector<ll>(m+1));
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            cin >> a[i][j];
        }
    }
    ll k;
    cin >> k;
    for (int i=1; i<=k; i++) {
        cin >> x[i] >> y[i];
        x[i]++; y[i]++;
    }
    ll answer = -1;
    vis.resize(n+1, vector<ll>(m+1, 0));
    for (int mask = 0; mask < (1 << (2*k)); mask++) {
        ll rmask = mask, ans = 0;
        for (int i=1; i<=k; i++) {
            ll type = mask%2 + (mask/2) % 2;
            mask >>= 2;
            ans += check(x[i], y[i], type);
            if (ans < 0) break;
        }
        for (int i=1; i<=k; i++) {
            vis[x[i]][y[i]] = vis[x[i]-1][y[i]] = vis[x[i]-1][y[i]-1] = vis[x[i]][y[i]-1] =
            vis[min(n, x[i]+1)][y[i]-1] = vis[min(n, x[i]+1)][y[i]] = vis[min(n, x[i]+1)][min(m, y[i]+1)] =
            vis[x[i]][min(m, y[i]+1)] = vis[x[i]-1][min(m, y[i]+1)] = 0;
        }
        mask = rmask;
        answer = max(answer, ans);
    }
    if (answer == -1) cout << "No" << el;
    else cout << answer << el;
}
signed main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    //freopen("nocross.in", "r", stdin);
    //freopen("nocross.out", "w", stdout);
    int ntest = 1;
    //cin >> ntest;
    while (ntest--){
        solve();
    }
    return 0;
}
;
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |