summaryrefslogtreecommitdiffstats
path: root/inf/lige/1/1.c
blob: 8bbfdaa4c68f8dd6ffcb2d8aed5b4c177fd7b9a9 (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
	int debug = 0;
	char buf[128];
	char buf2[128];
	fgets(buf, 128, stdin);
	char * x = buf;
	int m = strtol(x, &x, 10);
	x++;
	int n = strtol(x, NULL, 10);
	if (debug) fprintf(stderr, "m %d n %d\n", m, n);
	char * old = buf;
	char * cur = buf2;
	memset(old, '0', 128);
	memset(cur, '0', 128);
	unsigned mej = 0;
	while (!ferror(stdin) && !feof(stdin) && m--) {
		fgets(cur, 128, stdin);
		unsigned meja = 0;
		for (int i = 0; i < n-1; i++)
			if (cur[i] != cur[i+1])
				meja++;
		if (cur[n-1] == '1')
			meja++;
		if (cur[0] == '1')
			meja++;
		if (debug) fprintf(stderr, "vrsta. po ld je mej: %u\n", meja);
		mej += meja;
		meja = 0;
		for (int i = 0; i < n; i++)
			if (old[i] != cur[i])
				meja++;
		if (debug) fprintf(stderr, "\tpo gor dol je mej: %u\n", meja);
		mej += meja;
		meja = 0;
		if (!m)
			for (int i = 0; i < n; i++)
				if (cur[i] == '1')
					meja++;
		if (debug) fprintf(stderr, "\tpo zadnji je mej: %u\n", meja);
		mej += meja;
		meja = 0;
		char * tmp = old;
		old = cur;
		cur = tmp;
	}
	printf("%u\n", mej);
}