#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
void da(int *a, int s, int e) {
  for (int i = s; i < e; i++) cout << a[i] << " ";
  cout << '\n';
}
void db(string s) { cout << s << '\n'; }
void cp(int *a, int *b, int n) {
  for (int i = 0; i < n; i++) b[i] = a[i];
}
const long long sn = 250, LOG = 22, MOD = 4294967296, INF = 1 << 30, N = 1 << 8;
int dp[210][N], pre[210][N];
int arr[210][N], val[110][110], n, m, k, dx[4] = {0, 0, 1, -1},
                                         dy[4] = {1, -1, 0, 0};
queue<int> q;
char s[110][110];
void dfs(int pos, int st) {
  int x = pos / m, y = pos % m;
  s[x][y] = 'X';
  if (pre[pos][st] == -1) return;
  int pos2 = pre[pos][st] / 1000, pst = pre[pos][st] % 1000;
  dfs(pos2, pst);
  if (pst != st) dfs(pos2, st - pst);
}
void upd(int pos, int st, int v, int fa) {
  if (v > dp[pos][st]) return;
  dp[pos][st] = v;
  pre[pos][st] = fa;
  if (!arr[pos][st]) {
    q.push(pos), q.push(st);
    arr[pos][st] = 1;
  }
}
int main() {
  int x, y, x2, y2, pos, st, now;
  memset(arr, 0, sizeof(arr));
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      s[i][j] = '.';
      cin >> val[i][j];
    }
    s[i][m] = '\0';
  }
  int tmp = (1 << k);
  for (int i = 0; i < n * m; i++)
    for (int j = 0; j < tmp; j++) dp[i][j] = INF;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    x--, y--;
    upd(x * m + y, (1 << i), val[x][y], -1);
  }
  while (!q.empty()) {
    pos = q.front();
    q.pop();
    st = q.front();
    q.pop();
    arr[pos][st] = 0;
    x = pos / m, y = pos % m;
    for (int i = 0; i < 4; i++) {
      x2 = x + dx[i];
      y2 = y + dy[i];
      if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < m)
        upd(x2 * m + y2, st, dp[pos][st] + val[x2][y2], pos * 1000 + st);
    }
    now = tmp - 1 - st;
    for (int i = now; i; i = (i - 1) & now)
      upd(pos, st | i, dp[pos][st] + dp[pos][i] - val[x][y], pos * 1000 + st);
  }
  int ans = INF, res = 0;
  for (int i = 0; i < n * m; i++)
    if (dp[i][tmp - 1] < ans) res = i, ans = dp[i][tmp - 1];
  dfs(res, tmp - 1);
  cout << ans << '\n';
  for (int i = 0; i < n; i++) cout << s[i] << '\n';
}
