fork download
  1. # paiza POH! vol.2
  2. # result:
  3. # http://p...content-available-to-author-only...a.jp/poh/paizen/result/5921f33c8a94fee3caa44fe179df053f
  4. # author: Leonardone @ NEETSDKASU
  5. h, w = map(int, raw_input().split())
  6. sp = [0 for j in xrange(w)]
  7. tb = [[0 for j in xrange(w + 1)] for i in xrange(h + 1)]
  8. for y in xrange(h) :
  9. str = raw_input()
  10. for x in xrange(w) :
  11. if str[x] == '0' :
  12. sp[x] = sp[x] + 1
  13. s = sp[x]
  14. t = 1
  15. i = x
  16. while i >= 0 and sp[i] > 0 :
  17. if sp[i] < s :
  18. s = sp[i]
  19. tb[s][t] = tb[s][t] + 1
  20. t = t + 1
  21. i = i - 1
  22. else :
  23. sp[x] = 0
  24.  
  25. for x in xrange(1, w + 1) :
  26. for y in xrange(h - 1, 0, -1) :
  27. tb[y][x] = tb[y][x] + tb[y + 1][x]
  28.  
  29. n = int(raw_input())
  30. for i in xrange(n) :
  31. s, t = map(int, raw_input().split())
  32. if s <= h and t <= w :
  33. print tb[s][t]
  34. else :
  35. print "0"
Success #stdin #stdout 0.01s 7856KB
stdin
5 5
00000
00100
00010
10001
10000
7
2 2
1 1
3 2
3 2
2 3
3 1
1 3
stdout
6
20
2
2
1
6
7