Advertisement
manish

Untitled

Mar 21st, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. # ------------------- fast io --------------------
  2. import os
  3. import sys
  4. from io import BytesIO, IOBase
  5.  
  6. BUFSIZE = 8192
  7.  
  8.  
  9. class FastIO(IOBase):
  10. newlines = 0
  11.  
  12. def __init__(self, file):
  13. self._fd = file.fileno()
  14. self.buffer = BytesIO()
  15. self.writable = "x" in file.mode or "r" not in file.mode
  16. self.write = self.buffer.write if self.writable else None
  17.  
  18. def read(self):
  19. while True:
  20. b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
  21. if not b:
  22. break
  23. ptr = self.buffer.tell()
  24. self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
  25. self.newlines = 0
  26. return self.buffer.read()
  27.  
  28. def readline(self):
  29. while self.newlines == 0:
  30. b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
  31. self.newlines = b.count(b"\n") + (not b)
  32. ptr = self.buffer.tell()
  33. self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
  34. self.newlines -= 1
  35. return self.buffer.readline()
  36.  
  37. def flush(self):
  38. if self.writable:
  39. os.write(self._fd, self.buffer.getvalue())
  40. self.buffer.truncate(0), self.buffer.seek(0)
  41.  
  42.  
  43. class IOWrapper(IOBase):
  44. def __init__(self, file):
  45. self.buffer = FastIO(file)
  46. self.flush = self.buffer.flush
  47. self.writable = self.buffer.writable
  48. self.write = lambda s: self.buffer.write(s.encode("ascii"))
  49. self.read = lambda: self.buffer.read().decode("ascii")
  50. self.readline = lambda: self.buffer.readline().decode("ascii")
  51.  
  52.  
  53. sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
  54. input = lambda: sys.stdin.readline().rstrip("\r\n")
  55.  
  56. # ------------------- fast io --------------------
  57. import sys
  58. sys.setrecursionlimit(10**6)
  59.  
  60. def check(cnt, cnt2):
  61. # 7 5
  62. x,y = min(cnt, cnt2), max(cnt, cnt2)
  63. if x < 2:return 0
  64. # find greatest x such that 2x <= y
  65. k = max(0, min(y//2, x) - 1) + max(0, min(x//2, y) - 1)
  66. return k
  67.  
  68. for _ in range(int(input()) if True else 1):
  69. cs = "Case #"+str(_ + 1)+":"
  70. # n = int(input())
  71. n, m = map(int, input().split())
  72. # a, b = map(int, input().split())
  73. # c, d = map(int, input().split())
  74. # a = list(map(int, input().split()))
  75. # b = list(map(int, input().split()))
  76. a = []
  77. for i in range(n):
  78. a += [list(map(int, input().split()))]
  79. left = [[-1]*m for i in range(n)]
  80. right = [[-1]*m for i in range(n)]
  81. def checkright(i, j):
  82. if not 0 <= j < m:return 0
  83. if right[i][j] == -1:
  84. if a[i][j] == 0:
  85. right[i][j] = 0
  86. else:
  87. right[i][j] = 1 + checkright(i, j+1)
  88. return right[i][j]
  89. def checkleft(i, j):
  90. if not 0 <= j < m:return 0
  91. if left[i][j] == -1:
  92. if a[i][j] == 0:
  93. left[i][j] = 0
  94. else:
  95. left[i][j] = 1 + checkleft(i, j-1)
  96. return left[i][j]
  97.  
  98.  
  99.  
  100. ans = 0
  101. cnt = 0
  102. for j in range(m):
  103. cnt = 0
  104. for i in range(n):
  105. if a[i][j] == 1:
  106. cnt += 1
  107. else:
  108. cnt = 0
  109. if cnt >= 2:
  110. ans += check(cnt, checkright(i,j)) + check(cnt, checkleft(i, j))
  111. for j in range(m):
  112. cnt = 0
  113. for i in range(n - 1, -1, -1):
  114. if a[i][j] == 1:
  115. cnt += 1
  116. else:
  117. cnt = 0
  118. if cnt >= 2:
  119. ans += check(cnt, checkright(i,j)) + check(cnt, checkleft(i, j))
  120. print(cs+" " +str(ans))
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement