### inner classes

class BIT
  def initialize(n)
    @n = n
    @bits = (n + 1).times.map{0}
  end

  def sum(x)
    s = 0
    while x > 0
      s += @bits[x]
      x -= (x & -x)
    end
    s
  end

  def add(x, v)
    while x <= @n
      @bits[x] += v
      x += (x & -x)
    end
  end
end

class BIT2D
  def initialize(m, n)
    @m = m
    @bits = (m + 1).times.map{BIT.new(n)}
  end

  def sum(y, x)
    s = 0
    while y > 0
      s += @bits[y].sum(x)
      y -= (y & -y)
    end
    s
  end

  def add(y, x, v)
    while y <= @m
      @bits[y].add(x, v)
      y += (y & -y)
    end
  end
end

### main

m, n = gets.strip.split(' ').map{|s| s.to_i}
k = gets.strip.to_i

bit2ds = {}
bit2ds['J'] = BIT2D.new(m, n)
bit2ds['O'] = BIT2D.new(m, n)
bit2ds['I'] = BIT2D.new(m, n)

for y in (1..m)
  hl = gets.strip.split('')

  for x in (1..n)
    bit2ds[hl[x - 1]].add(y, x, 1)
  end
end

k.times do
  a, b, c, d = gets.strip.split(' ').map{|s| s.to_i}
  sums = []

  for ch in ['J', 'O', 'I']
    b2d = bit2ds[ch]
    sum =
      b2d.sum(c, d) - b2d.sum(a - 1, d) -
      b2d.sum(c, b - 1) + b2d.sum(a - 1, b - 1)

    sums << sum
  end

  puts sums.join(' ')
end