#include "bits/stdc++.h"

// Begin {{{
using namespace std;

#define all(x) x.begin(), x.end()
#define rep(i, n) for (i64 i = 0; i < (n); ++i)
#define reps(i, b, e) for (i64 i = (b); i <= (e); ++i)
#define repr(i, b, e) for (i64 i = (b); i >= (e); --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

#ifdef DEBUG
#define dump(...) dump_(#__VA_ARGS__, __VA_ARGS__);
#else
#define dump(...)
#endif

using i64 = int_fast64_t;
using pii = pair<i64, i64>;

template <class A, class B>
inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }

template <class A, class B>
inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int MOD = int(1e9) + 7;

// input
void input() {}
template <class Head, class... Tail>
void input(Head&& head, Tail&&... tail) {
  cin >> head;
  input(forward<Tail>(tail)...);
}

// outs
void outs() {}
template <class Head, class... Tail>
void outs(Head&& head, Tail&&... tail) {
  cout << head << " \n"[sizeof...(tail)==0];
  outs(forward<Tail>(tail)...);
}

template <class T>
void outs(vector<T> &vec) { for (auto &e : vec) cout << e << " \n"[&e==&vec.back()]; }

template <class T>
void outs(vector<vector<T>> &df) { for (auto &vec : df) OUTS(vec); }

// outl
void outl() {}
template <class Head, class... Tail>
void outl(Head&& head, Tail&&... tail) {
  cout << head << "\n";
  outl(forward<Tail>(tail)...);
}

template <class T>
void outl(vector<T> &vec) { for (auto &e : vec) cout << e << "\n"; }

// outn
void outn() {}
template <class Head, class... Tail>
void outn(Head&& head, Tail&&... tail) {
  cout << head;
  outn(forward<Tail>(tail)...);
}

template <class T>
void outn(vector<T> &vec) { for (auto &e : vec) cout << e; }

// dump
template <class T>
void dump_(const char *s, T&& x) {
  clog << '{';
  while(*s != '\0') clog << *(s++);
  clog << ": " << x << '}' << "\n";
}

template <class Head, class... Tail>
void dump_(const char *s, Head&& head, Tail&&... tail) {
  clog << '{';
  while(*s != ',') clog << *(s++);
  clog << ": " << head << "}, ";
  for (++s; !isgraph(*s); ++s);
  dump_(s, forward<Tail>(tail)...);
}
// }}} End

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  var(int, H, W, K);

  vector<vector<char>> mat(H + 10, vector<char>(W + 10));
  reps(i, 1, H) {
    reps(j, 1, W) {
      input(mat[i][j]);
    }
  }

  vector<vector<i64>> J(H + 10, vector<i64>(W + 10));
  vector<vector<i64>> O(H + 10, vector<i64>(W + 10));
  vector<vector<i64>> I(H + 10, vector<i64>(W + 10));
  reps(i, 1, H) {
    reps(j, 1, W) {
      J[i][j] = J[i-1][j] + J[i][j-1] - J[i-1][j-1] + (mat[i][j] == 'J' ? 1 : 0);
      O[i][j] = O[i-1][j] + O[i][j-1] - O[i-1][j-1] + (mat[i][j] == 'O' ? 1 : 0);
      I[i][j] = I[i-1][j] + I[i][j-1] - I[i-1][j-1] + (mat[i][j] == 'I' ? 1 : 0);
    }
  }

  rep(i, K) {
    var(int, sy, sx, gy, gx);
    i64 J_res = J[gy][gx] - J[gy][sx-1] - J[sy-1][gx] + J[sy-1][sx-1];
    i64 O_res = O[gy][gx] - O[gy][sx-1] - O[sy-1][gx] + O[sy-1][sx-1];
    i64 I_res = I[gy][gx] - I[gy][sx-1] - I[sy-1][gx] + I[sy-1][sx-1];

    outs(J_res, O_res, I_res);
  }

  return 0;
}



Measured memory_bytes: 27696