This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int infinity = 1e9;
vvi board, ind;
vii special;
int dir_x[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dir_y[] = {0, 0, -1, 1, 1, -1, 1, -1};
set<pii> q;
int ans = 0;
int Dfs(vvi &graph, int node, vb &visited) {
    int x = special[node].x, y = special[node].y;
    q.insert({x - 1, y}), q.insert({x + 1, y}), q.insert({x, y - 1}), q.insert({x, y + 1});
    ans += board[x][y];
    visited[node] = true;
    int sz = 1;
    for (int neighbor : graph[node]) {
        if (!visited[neighbor]) sz += Dfs(graph, neighbor, visited);
    }
    return sz;
}
int main() {
    int n, m;
    cin >> n >> m;
    board.resize(n, vi(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> board[i][j];
        }
    }
    ind.resize(n, vi(m));
    vvb block(n, vb(m));
    int k;
    cin >> k;
    special.resize(k);
    for (int i = 0; i < k; i++) {
        cin >> special[i].x >> special[i].y;
        ind[special[i].x][special[i].y] = i + 1;
        block[special[i].x][special[i].y] = true;
    }
    vb finished(k);
    while (true) {
        bool is_new = false;
        for (int i = 0; i < k; i++) {
            if (finished[i]) continue;
            int x = special[i].x, y = special[i].y;
            int neighbor1 = 4, sum = board[x][y];
            for (int j = 0; j < 4; j++) {
                x += dir_x[j], y += dir_y[j];
                if (0 <= x && x < n && 0 <= y && y < m && !block[x][y]) {
                    neighbor1--;
                    sum += board[x][y];
                }
                x -= dir_x[j], y -= dir_y[j];
            }
            int neighbor2 = 0;
            for (int j = 4; j < 8; j++) {
                x += dir_x[j], y += dir_y[j];
                if (0 <= x && x < n && 0 <= y && y < m && ind[x][y]) {
                    neighbor2++;
                }
                x -= dir_x[j], y -= dir_y[j];
            }
            if (neighbor1 > 1 || neighbor2 > 1) {
                cout << "No";
                return 0;
            }
            if (neighbor1 == 1) {
                ans += sum;
                finished[i] = true;
                is_new = true;
                for (int j = 0; j < 4; j++) {
                    x += dir_x[j], y += dir_y[j];
                    if (0 <= x && x < n && 0 <= y && y < m) block[x][y] = true;
                    x -= dir_x[j], y -= dir_y[j];
                }
                continue;
            }
            if (neighbor2) {
                block[x][y - 1] = true;
                block[x][y + 1] = true;
                ans += board[x][y - 1] + board[x][y + 1] + board[x][y];
                if (ind[x + 1][y + 1] || ind[x + 1][y - 1]) {
                    block[x - 1][y] = true;
                    ans += board[x - 1][y];
                } else {
                    block[x + 1][y] = true;
                    ans += board[x + 1][y];
                }
                finished[i] = true;
                is_new = true;
            }
        }
        if (!is_new) {
            break;
        }
    }
    vvi graph(k);
    for (int i = 0; i < k; i++) {
        if (finished[i]) continue;
        int x = special[i].x, y = special[i].y;
        if (x > 1 && ind[x - 2][y]) {
            graph[i].push_back(ind[x - 2][y] - 1);
        }
        if (y > 1 && ind[x][y - 2]) {
            graph[i].push_back(ind[x][y - 2] - 1);
        }
        if (x < n - 2 && ind[x + 2][y]) {
            graph[i].push_back(ind[x + 2][y] - 1);
        }
        if (y < m - 2 && ind[x][y + 2]) {
            graph[i].push_back(ind[x][y + 2] - 1);
        }
    }
    vb visited(k);
    for (int i = 0; i < k; i++) {
        if (finished[i] || visited[i]) continue;
        q.clear();
        int sz = Dfs(graph, i, visited);
        if (sz * 3 > q.size()) {
            cout << "No";
            return 0;
        }
        int min_val = infinity;
        for (pii p : q) {
            ans += board[p.x][p.y];
            chkmin(min_val, board[p.x][p.y]);
        }
        if (sz * 3 < q.size()) {
            ans -= min_val;
        }
    }
    cout << ans;
}
Compilation message (stderr)
covering.cpp: In function 'int main()':
covering.cpp:139:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |         if (sz * 3 > q.size()) {
      |             ~~~~~~~^~~~~~~~~~
covering.cpp:148:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |         if (sz * 3 < q.size()) {
      |             ~~~~~~~^~~~~~~~~~| # | 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... |