#include <bits/stdc++.h>
using namespace std;
int a[205][205];
int b[205];
int edge[205][205];
long long dis[205][205];
long long dp[131][205];
vector<pair<int, int> > la[131][205];
int imp[13];
int dila[205][205];
bool had[205];
void solve(int x1, int x2) {
  queue<pair<int, int> > q;
  q.push(make_pair(x1, x2));
  had[x1] = 1;
  had[x2] = 1;
  while (!q.empty()) {
    int a1 = q.front().first, b1 = q.front().second;
    q.pop();
    had[dila[a1][b1]] = 1;
    if (~dila[a1][b1]) {
      q.push(make_pair(a1, dila[a1][b1]));
      q.push(make_pair(dila[a1][b1], b1));
    }
  }
}
int main() {
  int n, m, K;
  cin >> n >> m >> K;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      b[i * m + j] = a[i][j];
      if (i - 1 >= 0)
        edge[(i - 1) * m + j][i * m + j] = edge[i * m + j][(i - 1) * m + j] = 1;
      if (i + 1 < n)
        edge[(i + 1) * m + j][i * m + j] = edge[i * m + j][(i + 1) * m + j] = 1;
      if (j - 1 >= 0)
        edge[i * m + j - 1][i * m + j] = edge[i * m + j][i * m + j - 1] = 1;
      if (j + 1 < m)
        edge[i * m + j + 1][i * m + j] = edge[i * m + j][i * m + j + 1] = 1;
    }
  }
  int al = n * m;
  memset(dila, -1, sizeof dila);
  for (int i = 0; i < al; i++) {
    for (int j = 0; j < al; j++) {
      if (edge[i][j])
        dis[i][j] = b[i] + b[j];
      else
        dis[i][j] = 1e9;
    }
    dis[i][i] = b[i];
  }
  for (int k = 0; k < al; k++) {
    for (int i = 0; i < al; i++) {
      for (int j = 0; j < al; j++) {
        if (dis[i][j] > dis[i][k] + dis[k][j] - b[k]) {
          dila[i][j] = k;
          dis[i][j] = dis[i][k] + dis[k][j] - b[k];
        }
      }
    }
  }
  for (int i = 0; i < K; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    imp[i] = x * m + y;
  }
  for (int j = 0; j < al; j++) {
    for (int k = 0; k < 1 << K; k++) {
      dp[k][j] = 1e9;
    }
  }
  for (int i = 0; i < K; i++) {
    dp[1 << i][imp[i]] = b[imp[i]];
    had[imp[i]] = 1;
  }
  dp[0][0] = 0;
  for (int i = 1; i < 1 << K; i++) {
    priority_queue<pair<int, int> > q;
    for (int j = 0; j < al; j++) {
      for (int k = (i - 1) & i; k; k = (k - 1) & i) {
        if (dp[i ^ k][j] + dp[k][j] - b[j] < dp[i][j]) {
          dp[i][j] = dp[i ^ k][j] + dp[k][j] - b[j];
          la[i][j].clear();
          la[i][j].push_back(make_pair(i ^ k, j));
          la[i][j].push_back(make_pair(k, j));
        }
      }
      if (dp[i][j] < 1e8) {
        q.push(make_pair(-dp[i][j], j));
      }
    }
    while (!q.empty()) {
      int x = -q.top().first, y = q.top().second;
      q.pop();
      for (int ii = 0; ii < al; ii++) {
        if (!edge[ii][y]) continue;
        if (x + b[ii] < dp[i][ii]) {
          la[i][ii].clear();
          la[i][ii].push_back(make_pair(i, y));
          dp[i][ii] = x + b[ii];
          q.push(make_pair(-dp[i][ii], ii));
        }
      }
    }
  }
  long long mi = 1e9, id;
  for (int i = 0; i < al; i++) {
    if (mi > dp[(1 << K) - 1][i]) {
      mi = dp[(1 << K) - 1][i];
      id = i;
    }
  }
  queue<pair<int, int> > q;
  q.push(make_pair((1 << K) - 1, id));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    had[y] = 1;
    for (int i = 0; i < la[x][y].size(); i++) {
      int x1 = la[x][y][i].first, y1 = la[x][y][i].second;
      q.push(make_pair(x1, y1));
    }
  }
  cout << mi << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (had[i * m + j])
        cout << 'X';
      else
        cout << '.';
    }
    cout << endl;
  }
}
