## Algorithm

Problem Name: 2 AD-HOC - beecrowd | 1514

# Contest

By Cristhian Bonilha, UTFPR Brazil

Timelimit: 1

Most of the programmers who come to write contests with programming exercises agree in four caracteristics that every contest should achieve. Although not all of them are always achieved, more is better. The caracterists are the following:

1. Nobody solved all the problems.
2. Every problem was solved by at least one person (not necessarily the same).
3. There is no problem solved by everyone.
4. Everyone solved at least one problem (not necessarily the same).

Rafael organized a contest a few days ago, and is worried about how many of these caracteristics he got to achieve with his contest.

Given the information about the contest, with the number of contestants, the number of problems, and which contestant solved which problem, find out the number of caracteristics that were achieved on this contest.

## Input

There will be several tests cases. Each test case starts with two integers N and M (3 ≤ N, M ≤ 100).

Then, there will be N lines with M integers each, where the integer on the i-th line and on the j-th column is 1 if the i-th contestant solved the j-th problem, or 0 otherwise.

The last test case is indicated when N = M = 0, which should not be processed.

## Output

For each test case, print one line with one integer, representing the amount of caracteristics achieved by the given contest.

 Sample Input Sample Output 3 3 1 1 0 0 1 0 0 0 0 3 3 1 1 0 0 1 0 0 0 1 0 0 2 4

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
#include <stdio.h>
#include <string.h>

#define true 1
#define false 0

unsigned s_grid[101];
unsigned p_grid[101];

int main(int argc, char **argv)
{

unsigned n, m;
unsigned i, j, tmp;
_Bool flag1, flag2, flag3, flag4;

while (scanf("%u %u", &n, &m), n && m)
{

flag1 = flag2 = flag3 = flag4 = true;
memset(p_grid, 0, sizeof(p_grid));
memset(s_grid, 0, sizeof(s_grid));

for (i = 0; i  <  n; ++i)
for (j = 0; j  <  m; ++j)
{

scanf("%u", &tmp);
s_grid[i] += tmp;
p_grid[j] += tmp;

}

for (i = 0; i  <  n; ++i)
{

if (s_grid[i] == m)
flag1 = false;

if (s_grid[i] == 0)
flag4 = false;

}

for (i = 0; i  <  m; ++i)
{

if (p_grid[i] == 0)
flag2 = false;

if (p_grid[i] == n)
flag3 =false;

}

printf("%u\n", flag1 + flag2 + flag3 + flag4);

}

return 0;

}
``````
Copy The Code &

Input

cmd
3 3
1 1 0
0 1 0
0 0 0
3 3
1 1 0
0 1 0
0 0 1
0 0

Output

cmd
2
4

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````
#include <cstdio>
#define MAXN 110
int problemasresolvidos[MAXN];
int main() {
int n, m;
while (scanf("%d %d", &n, &m) && (n || m)) {
for (int i = 0; i  <  m; i++) problemasresolvidos[i] = 0;
int condicao1 = 1, condicao2 = 1, condicao3 = 1, condicao4 = 1;
for (int i = 0; i  <  n; i++) {
int resolvidos = 0;
for (int j = 0; j  <  m; j++) {
int x;
scanf("%d", &x);
if (x) {
resolvidos++;
problemasresolvidos[j]++;
}
}
if (resolvidos == 0) condicao4 = 0;
if (resolvidos == m) condicao1 = 0;
}
for (int i = 0; i  <  m; i++) {
if (problemasresolvidos[i] == n) condicao3 = 0;
if (problemasresolvidos[i] == 0) condicao2 = 0;
}
printf("%d\n", condicao1 + condicao2 + condicao3 + condicao4);
}
return 0;
}
``````
Copy The Code &

Input

cmd
3 3
1 1 0
0 1 0
0 0 0
3 3
1 1 0
0 1 0
0 0 1
0 0

Output

cmd
2
4

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
while True:
n, m = [int(x) for x in input().split()]
if not n+m: break
p = []
c1 = c4 = True
for g in range(n):
e = [int(x) for x in input().split()]
p.append(e)
c = e.count(1)
if c == len(e): c1 = False
if c == 0: c4 = False
p = [[lin[i] for lin in p] for i in range(len(p[0]))]
c2 = c3 = True
for g in p:
if g.count(1) == 0: c2 = False
if g.count(0) == 0: c3 = False
print([c1, c2, c3, c4].count(True))
``````
Copy The Code &

Input

cmd
3 3
1 1 0
0 1 0
0 0 0
3 3
1 1 0
0 1 0
0 0 1
0 0

Output

cmd
2
4