Algorithm


D. Three Sons
time limit per test
1 second
memory limit per test
256 megabytes
input
input.txt
output
output.txt

Three sons inherited from their father a rectangular corn fiend divided into n × m squares. For each square we know how many tons of corn grows on it. The father, an old farmer did not love all three sons equally, which is why he bequeathed to divide his field into three parts containing AB and C tons of corn.

The field should be divided by two parallel lines. The lines should be parallel to one side of the field and to each other. The lines should go strictly between the squares of the field. Each resulting part of the field should consist of at least one square.

Your task is to find the number of ways to divide the field as is described above, that is, to mark two lines, dividing the field in three parts so that on one of the resulting parts grew A tons of corn, B on another one and C on the remaining one.

Input

The first line contains space-separated integers n and m — the sizes of the original (1 ≤ n, m ≤ 50, max(n, m) ≥ 3). Then the field's description follows: n lines, each containing m space-separated integers cij, (0 ≤ cij ≤ 100) — the number of tons of corn each square contains. The last line contains space-separated integers A, B, C (0 ≤ A, B, C ≤ 106).

Output

Print the answer to the problem: the number of ways to divide the father's field so that one of the resulting parts contained A tons of corn, another one contained B tons, and the remaining one contained C tons. If no such way exists, print 0.

Examples
input
Copy
3 3
1 1 1
1 1 1
1 1 1
3 3 3
output
Copy
2
input
Copy
2 5
1 1 1 1 1
2 2 2 2 2
3 6 6
output
Copy
3
input
Copy
3 3
1 2 3
3 1 2
2 3 1
5 6 7
output
Copy
0
Note

The lines dividing the field can be horizontal or vertical, but they should be parallel to each other.



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <cstdio>
#include <vector>
#include <algorithm>

int main(){

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    long n, m; scanf("%ld %ld", &n, &m);
    std::vector<std::vector<long> > a(n + 1, std::vector<long>(m + 1, 0));
    std::vector<std::vector<long> > s(n + 1, std::vector<long>(m + 1, 0));
    std::vector<long> c(3, 0);
    long cnt(0);

    for(long p = 1; p <= n; p++){
        for(long q = 1; q <= m; q++){
            scanf("%ld", &a[p][q]);
            s[p][q]=s[p][q-1]+s[p-1][q]-s[p-1][q-1]+a[p][q];
        }
    }

    scanf("%ld %ld %ld", &c[0], &c[1], &c[2]);
    sort(c.begin(),c.end());

    for(long p=1;p<n;p++){
        for(long q=p+1;q<n;q++){
            std::vector < long> u(3);
            u[0] = s[p][m]; u[1] = s[q][m]-s[p][m]; u[2] = s[n][m]-s[q][m];
            sort(u.begin(),u.end());
            if(std::equal(u.begin(),u.end(),c.begin())){cnt++;}
        }
    }

    for(long p = 1;p < m; p++){
        for(long q= p + 1; q < m; q++){
            std::vector<long> u(3);
            u[0] = s[n][p]; u[1] = s[n][q]-s[n][p]; u[2] = s[n][m]-s[n][q];
            sort(u.begin(),u.end());
            if(std::equal(u.begin(),u.end(),c.begin())){cnt++;}
        }
    }

    printf("%ld\n", cnt);

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 3
1 1 1
1 1 1
1 1 1
3 3 3

Output

x
+
cmd
2
Advertisements

Demonstration


Codeforces Solution-D. Three Sons-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+