#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int H,W; cin >> H >> W; swap(H,W); vector> A(H,vector(W)); for(auto &h : A) for(auto &w : h) cin >> w; vector> visited(H,vector(W)),already = visited; vector> dxy = {{-1,0},{0,1},{1,0},{0,-1}}; bool yes = false; auto dfs = [&](auto dfs,int x,int y,int bx = -1,int by = -1){ visited.at(x).at(y) = true; for(auto [dx,dy] : dxy){ int nx = x+dx,ny = y+dy; if(nx < 0 || ny < 0 || nx >= H || ny >= W) continue; if(already.at(nx).at(ny) || (nx == bx && ny == by) || A.at(x).at(y) != A.at(nx).at(ny)) continue; if(visited.at(nx).at(ny)){yes = true; return;} dfs(dfs,nx,ny,x,y); if(yes) return; } already.at(x).at(y) = true; }; for(int i=0; i