/C05030 - Ma trận điểm ảnh

<Problem>

https://code.ptit.edu.vn/student/question/C05030
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>


#define max(i, j) ((i > j) ? i : j)
#define min(i, j) ((i < j) ? i : j)
#define swap(i, j) (i = i + j, j = i - j, i = i - j)
#define LIM 1001

int main() {
	int t;
	scanf("%d", &t);

	while (t--) {
		int n, m;
		scanf("%d %d", &n, &m);

		getchar();

		int arr[LIM][LIM];

		char c[LIM];

		int row1[LIM] = { 0 };
		int col1[LIM] = { 0 };
		int row2[LIM] = { 0 };
		int col2[LIM] = { 0 };

		for (int i = 0; i < n; i++) {
			gets(c);
			for (int j = 0; j < m; j++) {
				arr[i][j] = c[j] - '0';
				if (arr[i][j] == 1) {
					row1[i]++;
					col1[j]++;
				}
				else if (arr[i][j] == 2) {
					row2[i]++;
					col2[j]++;
				}
			}
		}

		long long cnt = 0;

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] == 2) cnt += row1[i] * col1[j];
				else if (arr[i][j] == 1) cnt += row2[i] * col2[j];
			}
		}

		printf("%lld\n", cnt);
	}

	return 0;
}