LiuDai @ 2024-07-24 16:34:40
#include <bits/stdc++.h>
//#define int long long
//#define int __int128
//#pragma GCC optimize(O2)
//#define getchar() static_cast<char>(getchar())
//#define FILEIO
namespace IO {
template<typename T>
inline T &read(T &x) {
x = 0;
T f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (!(ch ^ 45)) f = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
x *= f;
return x;
}
template<typename First, typename ...Args>
inline void read(First &first, Args &...args) {
read(first);
read(args...);
}
class ReadClass {
public:
ReadClass() = default;
template<typename T>
inline ReadClass &operator()(T &x) {
read(x);
return *this;
}
template<typename ...Args>
inline ReadClass &operator()(Args &...args) {
read(args...);
return *this;
}
template<typename Element>
inline ReadClass &array(Element *p, size_t start, size_t end) {
for (size_t i = start; i < end; ++i) {
read(p[i]);
}
return *this;
}
};
template<typename T>
inline void write(T x) {
if (x < 0) putchar(45), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 ^ 48);
}
class WriteClass {
public:
WriteClass() = default;
template<typename T>
inline WriteClass &operator()(const T &x, const char c = ' ') {
write(x);
putchar(c);
return *this;
}
inline WriteClass &p(const char c) {
putchar(c);
return *this;
}
inline WriteClass &e() {
putchar(10);
return *this;
}
inline WriteClass &s() {
putchar(32);
return *this;
}
inline WriteClass &t() {
putchar(9);
return *this;
}
};
inline void endl() { putchar(10); }
inline void space() { putchar(32); }
inline void tab() { putchar(9); }
ReadClass Read = ReadClass();
WriteClass Write = WriteClass();
#define endl endl()
#define space space()
#define tab tab()
}
using namespace IO;
using namespace std;
const int N = 1000 + 10,
M = N * N,
dx[] = {0, 0, 1, -1, 1, 1, -1, -1},
dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
using namespace std;
int n, m, a, b, ans[N][N], tmp[8];
bool ma[N][N];
int bloc[M], bkptr;
queue<tuple<int, int>> que;
inline bool chk(const int &x, const int &y) {
return x > 0 && x <= n && y > 0 && y <= n && ans[x][y] == -1;
}
#define gett(tuplee, indexx) get<indexx>(tuplee)
inline void solve() {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (ans[i][j] != -1) continue;
que.emplace(i, j);
bloc[bkptr] = 1;
ans[i][j] = bkptr;
while (!que.empty()) {
auto elem = que.front();
que.pop();
for (int k = 0; k < 4; ++k) {
const int nx = gett(elem, 0) + dx[k],
ny = gett(elem, 1) + dy[k];
if (chk(nx, ny) && (ma[gett(elem, 0)][gett(elem, 1)] ^ ma[nx][ny])) {
bloc[bkptr]++;
ans[nx][ny] = bkptr;
que.emplace(nx, ny);
}
}
}
bkptr++;
}
}
}
#undef gett
signed main() {
#ifdef FILEIO
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
Read(n, m);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
ma[i][j] = getchar() == '1';
}
getchar();
}
memset(ans, -1, sizeof ans);
solve();
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= n; ++j) {
// Write(bloc[ans[i][j]]);
// }
// endl;
// }
for (int i = 0; i < m; ++i) {
Read.array(tmp, 0, 2);
write(bloc[ans[tmp[0]][tmp[1]]]);endl;
}
#ifdef FILEIO
fclose(stdin);
fclose(stdout);
#endif
return 0;
}