Buggy python submission (verdict: Time Limit Exceeded):
N,M,Q = map(int,input().split())
src = [input() for i in range(N)]
qs = [tuple(map(int,input().split())) for i in range(Q)]

blue_cum = [[0 for j in range(M+1)] for i in range(N+1)]
for i in range(N):
    for j in range(M):
        blue_cum[i+1][j+1] += blue_cum[i+1][j] + int(src[i][j])
for i in range(N):
    for j in range(M+1):
        blue_cum[i+1][j] += blue_cum[i][j]

h_edge = [[0 for j in range(M)] for i in range(N+1)]
for i in range(N):
    for j in range(M-1):
        h_edge[i+1][j+1] += h_edge[i+1][j] + int(src[i][j:j+2]=='11')
for i in range(N):
    for j in range(M):
        h_edge[i+1][j] += h_edge[i][j]

v_edge = [[0 for j in range(M+1)] for i in range(N)]
for i in range(N-1):
    for j in range(M):
        v_edge[i+1][j+1] += v_edge[i+1][j] + int(src[i][j]=='1' and src[i+1][j]=='1')
for i in range(N-1):
    for j in range(M+1):
        v_edge[i+1][j] += v_edge[i][j]

ans = []
for y1,x1,y2,x2 in qs:
    y1 -= 1
    x1 -= 1
    blue = blue_cum[y1][x1] - blue_cum[y1][x2] - blue_cum[y2][x1] + blue_cum[y2][x2]
    edge = h_edge[y1][x1] - h_edge[y1][x2-1] - h_edge[y2][x1] + h_edge[y2][x2-1]
    edge+= v_edge[y1][x1] - v_edge[y1][x2] - v_edge[y2-1][x1] + v_edge[y2-1][x2]
    ans.append(blue - edge)
print(*ans, sep='\n')

Fixed submission:
N,M,Q = map(int,input().split())
src = [input() for i in range(N)]
qs = [tuple(map(int,input().split())) for i in range(Q)]

blue_cum = [[0 for j in range(M+1)] for i in range(N+1)]
for i in range(N):
    for j in range(M):
        blue_cum[i+1][j+1] += blue_cum[i+1][j] + int(src[i][j])
for i in range(N):
    for j in range(M+1):
        blue_cum[i+1][j] += blue_cum[i][j]

h_edge = [[0 for j in range(M)] for i in range(N+1)]
for i in range(N):
    for j in range(M-1):
        h_edge[i+1][j+1] += h_edge[i+1][j] + int(src[i][j:j+2]=='11')
for i in range(N):
    for j in range(M):
        h_edge[i+1][j] += h_edge[i][j]

v_edge = [[0 for j in range(M+1)] for i in range(N)]
for i in range(N-1):
    for j in range(M):
        v_edge[i+1][j+1] += v_edge[i+1][j] + int(src[i][j]=='1' and src[i+1][j]=='1')
for i in range(N-1):
    for j in range(M+1):
        v_edge[i+1][j] += v_edge[i][j]

ans = []
for y1,x1,y2,x2 in qs:
    y1 -= 1
    x1 -= 1
    blue = blue_cum[y1][x1] - blue_cum[y1][x2] - blue_cum[y2][x1] + blue_cum[y2][x2]
    edge = h_edge[y1][x1] - h_edge[y1][x2-1] - h_edge[y2][x1] + h_edge[y2][x2-1]
    edge+= v_edge[y1][x1] - v_edge[y1][x2] - v_edge[y2-1][x1] + v_edge[y2-1][x2]
    ans.append(blue - edge)
print(*ans, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
def solve() :
  N,M,Q = raw_input().split()
  N = int(N)
  M = int(M)
  Q = int(Q)
  
  grid = []
  for i in range(N):
    raw = raw_input()
    grid.append(raw)
  qs = []
  for ii in range(Q):
    raw = raw_input()
    qs.append(raw)

  for q in qs:
    qq = q.split()
    #print 'q=',q
    y1 = int(qq[0]) - 1
    x1 = int(qq[1]) - 1
    y2 = int(qq[2]) - 1
    x2 = int(qq[3]) - 1
    w = x2 - x1 + 1
    h = y2 - y1 + 1

    mapp = [[0 for col in range(w)] for row in range(h)]
    eqs = 0
    next = 1
    for y in range(h):
      for x in range(w):
        c = grid[y+y1][x+x1]
        if c == '0': continue
        up = -1 if y == 0 else mapp[y-1][x]
        left = -1 if x == 0 else mapp[y][x-1]
        #print 'y=',y,'x=',x,'up=',up,',left=',left
        if up > 0:
          if left > 0:
            if up == left : mapp[y][x] = up
            else :
              #print 'eq! :up=',up,',left=',left
              eqs += 1
              mapp[y][x] = left
          else :
            mapp[y][x] = up
        else:
          if left > 0:
            mapp[y][x] = left
          else: 
            mapp[y][x] = next
            next += 1
        #print 'up=',up,',left=',left,',mapp[',y,'][',x,']=',mapp[y][x]
    #print 'mapp=',mapp

    #sett = set()
    #for y in range(h) :
    #  for x in range(w) :
    #    #print 'y,x=',y,x
    #    if(mapp[y][x] > 0):  sett.add(mapp[y][x])
    #print 'sett=',sett,',eqs=',eqs
    print next-1-eqs

if __name__ == '__main__' :
  solve()

Fixed submission:
N,M,Q = map(int,input().split())
src = [input() for i in range(N)]
qs = [tuple(map(int,input().split())) for i in range(Q)]

blue_cum = [[0 for j in range(M+1)] for i in range(N+1)]
for i in range(N):
    for j in range(M):
        blue_cum[i+1][j+1] += blue_cum[i+1][j] + int(src[i][j])
for i in range(N):
    for j in range(M+1):
        blue_cum[i+1][j] += blue_cum[i][j]

h_edge = [[0 for j in range(M)] for i in range(N+1)]
for i in range(N):
    for j in range(M-1):
        h_edge[i+1][j+1] += h_edge[i+1][j] + int(src[i][j:j+2]=='11')
for i in range(N):
    for j in range(M):
        h_edge[i+1][j] += h_edge[i][j]

v_edge = [[0 for j in range(M+1)] for i in range(N)]
for i in range(N-1):
    for j in range(M):
        v_edge[i+1][j+1] += v_edge[i+1][j] + int(src[i][j]=='1' and src[i+1][j]=='1')
for i in range(N-1):
    for j in range(M+1):
        v_edge[i+1][j] += v_edge[i][j]

ans = []
for y1,x1,y2,x2 in qs:
    y1 -= 1
    x1 -= 1
    blue = blue_cum[y1][x1] - blue_cum[y1][x2] - blue_cum[y2][x1] + blue_cum[y2][x2]
    edge = h_edge[y1][x1] - h_edge[y1][x2-1] - h_edge[y2][x1] + h_edge[y2][x2-1]
    edge+= v_edge[y1][x1] - v_edge[y1][x2] - v_edge[y2-1][x1] + v_edge[y2-1][x2]
    ans.append(blue - edge)
print(*ans, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
def solve() :
  N,M,Q = raw_input().split()
  N = int(N)
  M = int(M)
  Q = int(Q)
  
  grid = []
  for i in range(N):
    raw = raw_input()
    grid.append(raw)
  qs = []
  for ii in range(Q):
    raw = raw_input()
    qs.append(raw)

  for q in qs:
    qq = q.split()
    #print 'q=',q
    y1 = int(qq[0]) - 1
    x1 = int(qq[1]) - 1
    y2 = int(qq[2]) - 1
    x2 = int(qq[3]) - 1
    w = x2 - x1 + 1
    h = y2 - y1 + 1

    mapp = [[0 for col in range(w)] for row in range(h)]
    eqs = 0
    next = 1
    for y in range(h):
      for x in range(w):
        c = grid[y+y1][x+x1]
        if c == '0': continue
        up = -1 if y == 0 else mapp[y-1][x]
        left = -1 if x == 0 else mapp[y][x-1]
        #print 'y=',y,'x=',x,'up=',up,',left=',left
        if up > 0:
          if left > 0:
            if up == left : mapp[y][x] = up
            else :
              #print 'eq! :up=',up,',left=',left
              eqs += 1
              mapp[y][x] = left
          else :
            mapp[y][x] = up
        else:
          if left > 0:
            mapp[y][x] = left
          else: 
            mapp[y][x] = next
            next += 1
        #print 'up=',up,',left=',left,',mapp[',y,'][',x,']=',mapp[y][x]
    #print 'mapp=',mapp

    sett = set()
    for y in range(h) :
      for x in range(w) :
        #print 'y,x=',y,x
        if(mapp[y][x] > 0):  sett.add(mapp[y][x])
    #print 'sett=',sett,',eqs=',eqs
    print len(sett)-eqs

if __name__ == '__main__' :
  solve()

Fixed submission:
N,M,Q = map(int,input().split())
src = [input() for i in range(N)]
qs = [tuple(map(int,input().split())) for i in range(Q)]

blue_cum = [[0 for j in range(M+1)] for i in range(N+1)]
for i in range(N):
    for j in range(M):
        blue_cum[i+1][j+1] += blue_cum[i+1][j] + int(src[i][j])
for i in range(N):
    for j in range(M+1):
        blue_cum[i+1][j] += blue_cum[i][j]

h_edge = [[0 for j in range(M)] for i in range(N+1)]
for i in range(N):
    for j in range(M-1):
        h_edge[i+1][j+1] += h_edge[i+1][j] + int(src[i][j:j+2]=='11')
for i in range(N):
    for j in range(M):
        h_edge[i+1][j] += h_edge[i][j]

v_edge = [[0 for j in range(M+1)] for i in range(N)]
for i in range(N-1):
    for j in range(M):
        v_edge[i+1][j+1] += v_edge[i+1][j] + int(src[i][j]=='1' and src[i+1][j]=='1')
for i in range(N-1):
    for j in range(M+1):
        v_edge[i+1][j] += v_edge[i][j]

ans = []
for y1,x1,y2,x2 in qs:
    y1 -= 1
    x1 -= 1
    blue = blue_cum[y1][x1] - blue_cum[y1][x2] - blue_cum[y2][x1] + blue_cum[y2][x2]
    edge = h_edge[y1][x1] - h_edge[y1][x2-1] - h_edge[y2][x1] + h_edge[y2][x2-1]
    edge+= v_edge[y1][x1] - v_edge[y1][x2] - v_edge[y2-1][x1] + v_edge[y2-1][x2]
    ans.append(blue - edge)
print(*ans, sep='\n')