B. Spotlights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.

You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.

A position is good if two conditions hold:

  • there is no actor in the cell the spotlight is placed to;
  • there is at least one actor in the direction the spotlight projects.

Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.

Input

The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan.

The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.

Output

Print one integer — the number of good positions for placing the spotlight.

Examples
input
2 4
0 1 0 0
1 0 1 0
output
9
input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
output
20
Note

In the first example the following positions are good:

  1. the (1, 1) cell and right direction;
  2. the (1, 1) cell and down direction;
  3. the (1, 3) cell and left direction;
  4. the (1, 3) cell and down direction;
  5. the (1, 4) cell and left direction;
  6. the (2, 2) cell and left direction;
  7. the (2, 2) cell and up direction;
  8. the (2, 2) and right direction;
  9. the (2, 4) cell and left direction.

Therefore, there are 9 good positions in this example.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <stdio.h>
#include <vector>
 
#pragma warning (disable : 4996)
using namespace std;
 
int main() {
    int M, N;
    cin >> M >> N;
    vector<vector<int> > ary(M, vector<int>(N));
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++)
            scanf("%d"&ary[i][j]);
    }
    int result = 0;
    for (int i = 0; i < M; i++) {
        bool cool = false;
        for (int j = 0; j < N; j++) {
            if (ary[i][j] == 1)
                cool = true;
            else if (ary[i][j] == && cool == true)
                result++;
        }    
        cool = false;
        for (int j = N-1; j > -1; j--) {
            if (ary[i][j] == 1)
                cool = true;
            else if (ary[i][j] == && cool == true)
                result++;
        }
    }
    for (int j = 0; j < N; j++) {
        bool cool = false;
        for (int i = 0; i < M; i++) {
            if (ary[i][j] == 1)
                cool = true;
            else if (ary[i][j] == && cool == true)
                result++;
        }
        cool = false;
        for (int i = M - 1; i > -1; i--) {
            if (ary[i][j] == 1)
                cool = true;
            else if (ary[i][j] == && cool == true)
                result++;
        }
    }
    cout << result;
    return 0;
}
cs


+ Recent posts