This page is still under construction.

Parts of this page are still being built. What you see may change.

Image Processing

Interview

Time limit1sMemory limit512 MB

Summary
Read an N by M grid of RGB triples, binarize each pixel by whether its average meets threshold T, then count the connected components of 255 pixels under 4-directional adjacency.
Level

Medium4 of 10

Topics
BFS, Graph, Matrix, Implementation
Solved
No attempts yet

Problem

You are given a simple but tedious image processing assignment. Its specification is as follows.

A screen with height NN and width MM consists of N×MN \times M pixels, and the pixel at (i,j)(i, j) holds the values of three colors: Ri,jR_{i,j} (Red), Gi,jG_{i,j} (Green), and Bi,jB_{i,j} (Blue). Each color is represented by an integer between 0 and 255.

For every pixel, take the average of the three colors. If it is greater than or equal to the threshold TT, set the pixel value to 255; otherwise set it to 0. Store the results as a new screen.

In the new screen, a pixel with value 255 is recognized as an object. If pixels with value 255 are adjacent vertically or horizontally, they are recognized as the same object.

Write a program that determines how many objects are on the screen.

Input

The first line gives the height NN and width MM of the screen, separated by a space.

From the second line to the N+1N + 1-th line, the values Ri,jR_{i,j}, Gi,jG_{i,j}, Bi,jB_{i,j} of the pixels making up the ii-th row are given, separated by spaces, for a total of MM pixels.

The last line gives the threshold TT.

Output

Print the number of objects on the screen. If there are no objects, print 0.

Constraints

  • 1≤N,M≤1,0001 \le N, M \le 1,000
  • 0≤Ri,j,Gi,j,Bi,j≤2550 \le R_{i,j}, G_{i,j}, B_{i,j} \le 255, and Ri,j,Gi,j,Bi,jR_{i,j}, G_{i,j}, B_{i,j} are integers
  • 0≤T≤2550 \le T \le 255, and TT is an integer

Examples2

  1. Example 1

    Input
    3 3
    255 255 255 100 100 100 255 255 255
    100 100 100 255 255 255 100 100 100
    255 255 255 100 100 100 255 255 255
    101
    
    Expected output
    5
    
  2. Example 2

    Input
    2 2
    124 150 123 100 100 100
    103 103 103 183 5 3
    255
    
    Expected output
    0